PHPackages                             zenstruck/object-routing-bundle - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. zenstruck/object-routing-bundle

AbandonedArchivedSymfony-bundle

zenstruck/object-routing-bundle
===============================

Symfony2 Bundle that allows generating urls from objects (sf1 style).

v0.5.1(10y ago)9542MITPHP

Since Nov 7Pushed 10y ago2 watchersCompare

[ Source](https://github.com/kbond/ZenstruckObjectRoutingBundle)[ Packagist](https://packagist.org/packages/zenstruck/object-routing-bundle)[ Docs](http://zenstruck.com/projects/ZenstruckObjectRoutingBundle)[ RSS](/packages/zenstruck-object-routing-bundle/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (6)Dependencies (10)Versions (8)Used By (0)

ZenstruckObjectRoutingBundle
============================

[](#zenstruckobjectroutingbundle)

[![Build Status](https://camo.githubusercontent.com/8a690e5c853f7371437a87ac0ff428fe2f937f952509d69e8a9e1eb769c51e90/687474703a2f2f696d672e736869656c64732e696f2f7472617669732f6b626f6e642f5a656e73747275636b4f626a656374526f7574696e6742756e646c652e7376673f7374796c653d666c61742d737175617265)](https://travis-ci.org/kbond/ZenstruckObjectRoutingBundle)[![Scrutinizer Code Quality](https://camo.githubusercontent.com/48c5bb6c6448eb2179a397716808a9c1e6cd15a7f1c1f6ba90dd603fd68384c4/687474703a2f2f696d672e736869656c64732e696f2f7363727574696e697a65722f672f6b626f6e642f5a656e73747275636b4f626a656374526f7574696e6742756e646c652e7376673f7374796c653d666c61742d737175617265)](https://scrutinizer-ci.com/g/kbond/ZenstruckObjectRoutingBundle/)[![Code Coverage](https://camo.githubusercontent.com/20704cc4872f0a44d5700c2f6d95ec3141d5cc655d2ea02a416c1a3aa1ddedea/687474703a2f2f696d672e736869656c64732e696f2f7363727574696e697a65722f636f7665726167652f672f6b626f6e642f5a656e73747275636b4f626a656374526f7574696e6742756e646c652e7376673f7374796c653d666c61742d737175617265)](https://scrutinizer-ci.com/g/kbond/ZenstruckObjectRoutingBundle/)[![SensioLabs Insight](https://camo.githubusercontent.com/f55a395c2922d7a56166ec1d4cf6891a2261b96bfe414a55942350a73c6664e4/68747470733a2f2f696d672e736869656c64732e696f2f73656e73696f6c6162732f692f32366132393461612d326131612d346465612d613164372d3234623861333232323630322e7376673f7374796c653d666c61742d737175617265)](https://insight.sensiolabs.com/projects/26a294aa-2a1a-4dea-a1d7-24b8a3222602)[![StyleCI](https://camo.githubusercontent.com/514e6166623325688b8e5217c0c9065112bcc9e2ae0dc8a52bafa4260798621f/68747470733a2f2f7374796c6563692e696f2f7265706f732f32363239303431322f736869656c64)](https://styleci.io/repos/26290412)[![Latest Stable Version](https://camo.githubusercontent.com/70a95fee44097a50dddc47f486189340f9a0c266fe4c0710b2474d19de2037a1/687474703a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f7a656e73747275636b2f6f626a6563742d726f7574696e672d62756e646c652e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/zenstruck/object-routing-bundle)[![License](https://camo.githubusercontent.com/8b723dab5b1db22081175e8f3e09fc4a5504ce01099325a505879753eab96f88/687474703a2f2f696d672e736869656c64732e696f2f7061636b61676973742f6c2f7a656e73747275636b2f6f626a6563742d726f7574696e672d62756e646c652e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/zenstruck/object-routing-bundle)

A Symfony Bundle that enables passing objects to your router. It works by decorating the default router with a custom `ObjectRouter` that transforms objects into a *route name* and *route parameters*. These are passed to the default router.

For those that remember symfony 1, this bundle brings back functionality that was [available in that framework](http://symfony.com/legacy/doc/jobeet/1_4/en/05?orm=Propel#chapter_05_object_route_class).

Installation
------------

[](#installation)

1. Install with composer:

    ```
     composer require zenstruck/object-routing-bundle

    ```
2. Enable the bundle in the kernel:

    ```
    // app/AppKernel.php

    public function registerBundles()
    {
        $bundles = array(
            // ...
            new Zenstruck\ObjectRoutingBundle\ZenstruckObjectRoutingBundle(),
        );
    }
    ```

Usage
-----

[](#usage)

To use this bundle, you must first have an object. Let's use a `BlogPost` example:

```
namespace Acme\Entity;

class BlogPost
{
    private $id;
    private $slug;
    private $body;

    public function getId()
    {
        return $this->id;
    }

    public function getSlug()
    {
        return $this->slug;
    }

    public function getBody()
    {
        return $this->body;
    }
}
```

Next, you must have some routes for that object:

```
# app/config/routing.yml

blog_post_show:
    pattern:  /blog/{id}-{slug}
    defaults: { _controller: AcmeBundle:BlogPost:show }

blog_post_edit:
    pattern:  /blog/{id}/edit
    defaults: { _controller: AcmeBundle:BlogPost:delete }

blog_post_delete:
    pattern:  /blog/{id}
    defaults: { _controller: AcmeBundle:BlogPost:delete }
    methods:  [DELETE]
```

### Without this bundle

[](#without-this-bundle)

Now, suppose you want to generate a route for a blog post. The *standard* way of doing this is as follows:

**Twig**:

```
{# variable "post" is an instance of "BlogPost" with id=1, slug=example #}

{{ path('blog_post_show', { id: post.id, slug: post.slug }) }} {# /blog/1-example #}
{{ path('blog_post_show', { id: post.id, slug: post.slug, view: full }, true) }} {# http://example.com/blog/1-example?view=full #}
{{ path('blog_post_edit', { id: post.id }) }} {# /blog/1/edit #}
{{ path('blog_post_delete', { id: post.id }) }} {# /blog/1 #}
```

**Symfony Controller**:

```
namespace Acme\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;

class MyController extends Controller
{
    public function myAction()
    {
        $post = // instance of Acme\Entity\BlogPost with id=1, path=example

        // blog_post_show (/blog/1-example)
        $url = $this->generateUrl('blog_post_show', ['id' => $post->getId(), 'slug' => $post->getSlug()]);

        // blog_post_show with extra parameter and absolute (http://example.com/blog/1-example?view=full)
        $url = $this->generateUrl(
            'blog_post_show',
            ['id' => $post->getId(), 'slug' => $post->getSlug(), 'view' => 'full'],
            true
        );

        // blog_post_edit (/blog/1/edit)
        $url = $this->generateUrl('blog_post_edit', ['id' => $post->getId()]);

        // blog_post_delete (/blog/1)
        $url = $this->generateUrl('blog_post_delete', ['id' => $post->getId()]);
    }
}
```

### With this bundle

[](#with-this-bundle)

In this bundle's config, setup a mapping of `BlogPost` to the blog post routes:

```
# app/config/config.yml

zenstruck_object_routing:
    class_map:
        Acme\Entity\BlogPost:
            default_route: blog_post_show
            default_parameters: [id]
            routes:
                blog_post_show: [id, path]
                blog_post_edit: ~
                blog_post_delete: ~
```

Generating routes for a blog post is now much simpler:

**Twig**:

```
{# variable "post" is an instance of "BlogPost" with id=1, slug=example #}

{{ path(post) }} {# /blog/1-example (blog_post_show url because it is the default route) #}
{{ path('blog_post_show', post) }} {# equivalent to above #}
{{ path(post, { view: full }, true) }} {# http://example.com/blog/1-example?view=full #}
{{ path('blog_post_show', post, { view: full }, true) }} {# equivalent to above #}
{{ path('blog_post_edit', post) }} {# /blog/1/edit #}
{{ path('blog_post_delete', post) }} {# /blog/1 #}
```

**Symfony Controller**:

```
namespace Acme\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;

class MyController extends Controller
{
    public function myAction()
    {
        $post = // instance of Acme\Entity\BlogPost with id=1, slug=example

        // blog_post_show (/blog/1-example)
        $url = $this->generateUrl($post); // blog_post_show url because it is the default route
        $url = $this->generateUrl('blog_post_show', $post); // equivalent to above

        // blog_post_show with extra parameter and absolute (http://example.com/blog/1-example?view=full)
        $url = $this->generateUrl($post, ['view' => 'full'], true);
        $url = $this->generateUrl('blog_post_show', $post, ['view' => 'full'], true); // equivalent to above

        // blog_post_edit (/blog/1/edit)
        $url = $this->generateUrl('blog_post_edit', $post);

        // blog_post_delete (/blog/1)
        $url = $this->generateUrl('blog_post_delete', $post);
    }
}
```

Custom Transformations
----------------------

[](#custom-transformations)

This bundle comes with a `ClassMapObjectTransformer` that uses the bundle's config to map object classes to routes. If you have a more complex scenario, you can add your own transformers. Simply have your custom transformer implement `Zenstruck\ObjectRoutingBundle\ObjectTransformer\ObjectTransformer` and register it as a service tagged with `zenstruck_object_routing.object_transformer`.

See `Zenstruck\ObjectRoutingBundle\ObjectTransformer\ClassMapObjectTransformer` for a reference.

Full Default Config
-------------------

[](#full-default-config)

```
zenstruck_object_routing:
    class_map:

        # Prototype
        class:

            # Optional - The route to use when an object is passed as the 1st parameter of Router::generate()
            default_route:        null

            # Route parameter as key, object method/public property as value (can omit key if object method/property is the same)
            default_parameters:

                # Examples:
                - id
                - path

            # Route name as key, parameter array as value (can leave parameter array as null if same as default_parameters)
            routes:

                # Examples:
                blog_show:           ~
                blog_edit:
                    - id

                # Prototype
                route_name:           []
```

**NOTE 1**: This bundle's router uses the `PropertyAccess` component to access the object's properties/methods.

**NOTE 2**: When mapping multiple objects that inherit one another, be sure to order them from child to parent. For instance, if you had a `BlogPost` that has a parent class of `Page` and both are mapped, be sure to put `BlogPost`before `Page`.

###  Health Score

29

—

LowBetter than 59% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity18

Limited adoption so far

Community8

Small or concentrated contributor base

Maturity58

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~80 days

Recently: every ~100 days

Total

6

Last Release

3805d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/707369cc916e0ea1aacbf077dcba464f611cef879f024d8944311a54a15224b3?d=identicon)[kbond](/maintainers/kbond)

---

Top Contributors

[![kbond](https://avatars.githubusercontent.com/u/127811?v=4)](https://github.com/kbond "kbond (23 commits)")

---

Tags

bundleSymfony2routing

### Embed Badge

![Health badge](/badges/zenstruck-object-routing-bundle/health.svg)

```
[![Health](https://phpackages.com/badges/zenstruck-object-routing-bundle/health.svg)](https://phpackages.com/packages/zenstruck-object-routing-bundle)
```

###  Alternatives

[sylius/sylius

E-Commerce platform for PHP, based on Symfony framework.

8.4k5.6M651](/packages/sylius-sylius)[shopware/platform

The Shopware e-commerce core

3.3k1.5M3](/packages/shopware-platform)[sulu/sulu

Core framework that implements the functionality of the Sulu content management system

1.3k1.3M152](/packages/sulu-sulu)[prestashop/prestashop

PrestaShop is an Open Source e-commerce platform, committed to providing the best shopping cart experience for both merchants and customers.

9.0k15.4k](/packages/prestashop-prestashop)[contao/core-bundle

Contao Open Source CMS

1231.6M2.4k](/packages/contao-core-bundle)[shopware/core

Shopware platform is the core for all Shopware ecommerce products.

595.2M386](/packages/shopware-core)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
