Table Of Contents

Support

You can obtain free community support for example through stackoverflow.

If you think you found a bug, please create a ticket in the bug tracker.


Continuous Inspections

If you take code quality seriously, try out the new continuous inspection service.
scrutinizer-ci.com

GitHub

Object Routing Library

This library makes generating routes for objects a breeze, and is not tied to any concrete router implementation. As part of the library, we ship an adapter for Symfony?s router. For Symfony router version <2.2 use JMS/ObjectRouting/Symfony/Symfony21Adapter and for a version >=2.2 use JMS/ObjectRouting/Symfony/Symfony22Adapter.

Installation

You can install this library through composer:

composer require jms/object-routing

or add it to your composer.json file directly.

Usage

Annotations

use JMS\ObjectRouting\Annotation\ObjectRoute;

/**
 * @ObjectRoute(type = "view", name = "the-actual-route-name", params = {
 *     "slug": "slug",
 * })
 */
class BlogPost
{
    public function getSlug()
    {
        /** .. */
    }
}

Php

<?php
// file: Acme.Model.BlogPost.php
$metadata = new JMS\ObjectRouting\Metadata\ClassMetadata('Acme\Model\BlogPost');

$metadata->addRoute('view', 'the-actual-route-name', array('slug' => 'slug'));

return $metadata;

Yaml

file: Acme.Model.BlogPost.yml:

Acme\Model\BlogPost:
    view:
        name: "the-actual-route-name"
        params:
            slug: "slug"

Xml

<!-- file: Acme.Model.BlogPost.xml -->
<?xml version="1.0" encoding="UTF-8" ?>
<object-routing>
    <class name="Acme\Model\BlogPost">
        <route type="view" name="the-actual-route-name">
            <param name="slug">slug</param>
        </route>
    </class>
</object-routing>

Route parameters are key-value pairs where keys represent the placeholder in the URL template, and values can be any value that is supported by Symfony2?s PropertyAccess Component.

If you are using Symfony2 and you defined a route like this:

class BlogPostController
{
    /**
     * @Route("/blog-posts/{slug}", name = "the-actual-route-name")
     */
    public function viewAction(BlogPost $post)
    {
    }
}

you can generate this route with the object router very easily:

$objectRouter->generate('view', $blogPost);
// equivalent to
$router->generate('the-actual-route-name', array('slug' => $blogPost->getSlug()));

For Twig, this library also provides two new functions:

{{ object_path('view', blogPost) }}
{# equivalent to #}
{{ path('the-actual-route-name', {'slug': blogPost.slug}) }}

{{ object_url('view', blogPost) }}
{# equivalent to #}
{{ url('the-actual-route-name', {'slug': blogPost.slug}) }}

For compatibility reason this library is shipped with two Twig extensions. If you are using Twig 1.* JMS/ObjectRouting/Twig/RoutingExtension will fit your needs and if you need support for Twig 2.* you can use JMS/ObjectRouting/Twig/Routing20Extension.

License

The code is released under the business-friendly Apache2 license.

Documentation is subject to the Attribution-NonCommercial-NoDerivs 3.0 Unported license.