|
| 1 | +.. index:: |
| 2 | + single: Definitions; RoutingAutoBundle |
| 3 | + |
| 4 | +Multiple Auto Routes |
| 5 | +==================== |
| 6 | + |
| 7 | +.. versionadded: 2.0 |
| 8 | +
|
| 9 | + The capability to add multiple routes for a managed object has been |
| 10 | + introduced in RoutingAutoBundle 2.0. |
| 11 | +
|
| 12 | +In the introduction you were shown an example using a single auto route schema |
| 13 | +definition. It is possible to have multiple auto-route definitions for each of your |
| 14 | +managed objects, and therefore multiple routes. |
| 15 | + |
| 16 | +Below we will modify the example given in the :doc:`Introduction |
| 17 | +<introduction>` and add a |
| 18 | +new ``edit`` schema definition which has the ``/admin`` prefix: |
| 19 | + |
| 20 | +.. configuration-block:: |
| 21 | + |
| 22 | + .. code-block:: yaml |
| 23 | +
|
| 24 | + # src/Acme/ForumBundle/Resources/config/cmf_routing_auto.yml |
| 25 | + Acme\ForumBundle\Document\Topic: |
| 26 | + definitions: |
| 27 | + main: |
| 28 | + uri_schema: /my-forum/{category}/{title} |
| 29 | + defaults: |
| 30 | + type: view |
| 31 | + edit: |
| 32 | + uri_schema: /admin/my-forum/{category}/{title} |
| 33 | + defaults: |
| 34 | + type: edit |
| 35 | + token_providers: |
| 36 | + category: [content_method, { method: getCategoryTitle, slugify: true }] |
| 37 | + title: [content_method, { method: getTitle }] # slugify is true by default |
| 38 | +
|
| 39 | + .. code-block:: xml |
| 40 | +
|
| 41 | + <!-- src/Acme/ForumBundle/Resources/config/cmf_routing_auto.xml --> |
| 42 | + <?xml version="1.0" ?> |
| 43 | + <auto-mapping xmlns="http://cmf.symfony.com/schema/routing_auto"> |
| 44 | + <mapping class="Acme\ForumBundle\Document\Topic"> |
| 45 | + <definition name="main" uri-schema="/my-forum/{category}/{title}"> |
| 46 | + <default key="type">view</default> |
| 47 | + </definition> |
| 48 | +
|
| 49 | + <definition name="edit" uri-schema="/admin/my-forum/{category}/{title}"> |
| 50 | + <default key="type">edit</default> |
| 51 | + </definition> |
| 52 | +
|
| 53 | + <token-provider token="category" name="content_method"> |
| 54 | + <option name="method">getCategoryName</option> |
| 55 | + <option name="slugify">true</option> |
| 56 | + </token-provider> |
| 57 | +
|
| 58 | + <token-provider token="title" name="content_method"> |
| 59 | + <option name="method">getTitle</option> |
| 60 | + </token-provider> |
| 61 | + </mapping> |
| 62 | + </auto-mapping> |
| 63 | +
|
| 64 | +Note that we specify the ``defaults``. The ``defaults`` key can be used to |
| 65 | +persist key / value information on a route. This information can later be used |
| 66 | +for many things, but importantly it can be used by the :doc:`Dynamic Router |
| 67 | +<../routing/dynamic>` to determine which controller should be used to handle |
| 68 | +the request. |
| 69 | + |
| 70 | +For example, the dynamic router could be configured as follows: |
| 71 | + |
| 72 | +.. configuration-block:: |
| 73 | + |
| 74 | + .. code-block:: yaml |
| 75 | +
|
| 76 | + # app/config/config.yml |
| 77 | + cmf_routing: |
| 78 | + # ... |
| 79 | + dynamic: |
| 80 | + # ... |
| 81 | + controllers_by_type: |
| 82 | + view: Acme\\BasicCmsBundle\\Controller\\ViewController |
| 83 | + edit: Acme\\BasicCmsBundle\\Controller\\ViewController |
| 84 | +
|
| 85 | + .. code-block:: xml |
| 86 | +
|
| 87 | + <!-- app/config/config.xml --> |
| 88 | + <?xml version="1.0" encoding="UTF-8" ?> |
| 89 | + <container xmlns="http://symfony.com/schema/dic/services"> |
| 90 | + <config xmlns="http://cmf.symfony.com/schema/dic/routing"> |
| 91 | + <! -- ... --> |
| 92 | + <dynamic> |
| 93 | + <! -- ... --> |
| 94 | + <controller-by-type |
| 95 | + type="view"> |
| 96 | + Acme\\BasicCmsBundle\\Controller\\ViewController |
| 97 | + </controller-by-type> |
| 98 | + <controller-by-type |
| 99 | + type="edit"> |
| 100 | + Acme\\BasicCmsBundle\\Controller\\EditController |
| 101 | + </controller-by-type> |
| 102 | + </dynamic> |
| 103 | + </config> |
| 104 | + </container> |
0 commit comments