|
| 1 | +Routing and Automatic Routing |
| 2 | +----------------------------- |
| 3 | + |
| 4 | +The routes (URLs) to your content will be automatically created and updated |
| 5 | +using the RoutingAutoBundle. This bundle uses a configuration language to |
| 6 | +specify automatic creation of routes, which can be a bit hard to grasp the |
| 7 | +first time you see it. |
| 8 | + |
| 9 | +For a full a full explanation refer to the |
| 10 | +:doc:`../../bundles/routing_auto/index`. |
| 11 | + |
| 12 | +In summary, you will configure the auto routing system to create a new auto |
| 13 | +routing document in the routing tree for every post or content created. The |
| 14 | +new route will be linked back to the target content: |
| 15 | + |
| 16 | +.. image:: ../../_images/cookbook/basic-cms-objects.png |
| 17 | + |
| 18 | +The paths above represent the path in the PHPCR-ODM document tree. In the next |
| 19 | +section you will define ``/cms/routes`` as the base path for routes, and subsequently |
| 20 | +the contents will be avilable at the following URLs: |
| 21 | + |
| 22 | +* **Home**: ``http://localhost:8000/page/home`` |
| 23 | +* **About**: ``http://localhost:8000/page/about`` |
| 24 | +* etc. |
| 25 | + |
| 26 | +Installation |
| 27 | +~~~~~~~~~~~~ |
| 28 | + |
| 29 | +Ensure that you have the following package installed: |
| 30 | + |
| 31 | +.. code-block:: javascript |
| 32 | +
|
| 33 | + { |
| 34 | + ... |
| 35 | + require: { |
| 36 | + ... |
| 37 | + "symfony-cmf/routing-auto-bundle": "1.0.*@alpha" |
| 38 | + }, |
| 39 | + ... |
| 40 | + } |
| 41 | +
|
| 42 | +.. note:: |
| 43 | + |
| 44 | + You are installing the bleeding edge version of the routing-auto bundle. |
| 45 | + |
| 46 | +Enable the routing bundles to your kernel:: |
| 47 | + |
| 48 | + class AppKernel extends Kernel |
| 49 | + { |
| 50 | + public function registerBundles() |
| 51 | + { |
| 52 | + $bundles = array( |
| 53 | + // ... |
| 54 | + new Symfony\Cmf\Bundle\RoutingBundle\CmfRoutingBundle(), |
| 55 | + new Symfony\Cmf\Bundle\RoutingAutoBundle\CmfRoutingAutoBundle(), |
| 56 | + ); |
| 57 | + |
| 58 | + // ... |
| 59 | + } |
| 60 | + } |
| 61 | + |
| 62 | +.. note:: |
| 63 | + |
| 64 | + The `symfony-cmf/routing-bundle` package is installed automatically as |
| 65 | + `symfony-cmf/routing-auto-bundle` depends on it. |
| 66 | + |
| 67 | +Enable the Dynamic Router |
| 68 | +~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 69 | + |
| 70 | +The RoutingAutoBundle uses the CMF `RoutingBundle`_ which enables routes to |
| 71 | +be provided from a database (in addition to being provided from |
| 72 | +the routing configuration files as in core Symfony 2). |
| 73 | + |
| 74 | +Add the following to your application configuration: |
| 75 | + |
| 76 | +.. configuration-block:: |
| 77 | + |
| 78 | + .. code-block:: yaml |
| 79 | +
|
| 80 | + # /app/config/config.yml |
| 81 | + cmf_routing: |
| 82 | + chain: |
| 83 | + routers_by_id: |
| 84 | + cmf_routing.dynamic_router: 20 |
| 85 | + router.default: 100 |
| 86 | + dynamic: |
| 87 | + enabled: true |
| 88 | + persistence: |
| 89 | + phpcr: |
| 90 | + route_basepath: /cms/routes |
| 91 | +
|
| 92 | + .. code-block:: xml |
| 93 | +
|
| 94 | + <!-- app/config/config.xml --> |
| 95 | + <container xmlns="http://symfony.com/schema/dic/services"> |
| 96 | + <config xmlns="http://cmf.symfony.com/schema/dic/routing"> |
| 97 | + <chain> |
| 98 | + <router-by-id id="cmf_routing.dynamic_router">20</router-by-id> |
| 99 | + <router-by-id id="router.default">100</router-by-id> |
| 100 | + </chain> |
| 101 | + <dynamic> |
| 102 | + <persistence> |
| 103 | + <phpcr route-basepath="/cms/routes" /> |
| 104 | + </persistence> |
| 105 | + </dynamic> |
| 106 | + </config> |
| 107 | + </container> |
| 108 | +
|
| 109 | + .. code-block:: php |
| 110 | +
|
| 111 | + // app/config/config.php |
| 112 | + $container->loadFromExtension('cmf_routing', array( |
| 113 | + 'dynamic' => array( |
| 114 | + 'persistence' => array( |
| 115 | + 'phpcr' => array( |
| 116 | + 'enabled' => true, |
| 117 | + 'route_basepath' => '/cms/routes', |
| 118 | + ), |
| 119 | + ), |
| 120 | + ), |
| 121 | + )); |
| 122 | +
|
| 123 | +This will: |
| 124 | + |
| 125 | +#. Cause the default Symfony router to be replaced by the chain router. The |
| 126 | + chain router enables you to have multiple routers in your application. You |
| 127 | + add the dynamic router (which can retrieve routes from the database) and |
| 128 | + the default Symfony router (which retrieves routes from configuration |
| 129 | + files). The number indicates the order of precedence - the router with the |
| 130 | + lowest number will be called first; |
| 131 | +#. Configure the **dynamic** router which you have added to the router chain. |
| 132 | + You specify that it should use the PHPCR backend and that the *root* route |
| 133 | + can be found at ``/cms/routes``. |
| 134 | + |
| 135 | +Auto Routing Configuration |
| 136 | +~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 137 | + |
| 138 | +Create the following file in your applications configuration directory: |
| 139 | + |
| 140 | +.. code-block:: yaml |
| 141 | +
|
| 142 | + # app/config/routing_auto.yml |
| 143 | + cmf_routing_auto: |
| 144 | + mappings: |
| 145 | + Acme\BasicCmsBundle\Document\Page: |
| 146 | + content_path: |
| 147 | + pages: |
| 148 | + provider: [specified, { path: /cms/routes/page }] |
| 149 | + exists_action: use |
| 150 | + not_exists_action: create |
| 151 | + content_name: |
| 152 | + provider: [content_method, { method: getTitle }] |
| 153 | + exists_action: auto_increment |
| 154 | + not_exists_action: create |
| 155 | +
|
| 156 | + Acme\BasicCmsBundle\Document\Post: |
| 157 | + content_path: |
| 158 | + blog_path: |
| 159 | + provider: [specified, { path: /cms/routes/post }] |
| 160 | + exists_action: use |
| 161 | + not_exists_action: create |
| 162 | + date: |
| 163 | + provider: [content_datetime, { method: getDate}] |
| 164 | + exists_action: use |
| 165 | + not_exists_action: create |
| 166 | + content_name: |
| 167 | + provider: [content_method, { method: getTitle }] |
| 168 | + exists_action: auto_increment |
| 169 | + not_exists_action: create |
| 170 | +
|
| 171 | +This will configure the routing auto system to automatically create and update |
| 172 | +route documents for both the ``Page`` and ``Post`` documents. |
| 173 | + |
| 174 | +In summary: |
| 175 | + |
| 176 | +* The ``content_path`` key represents the parent path of the content, e.g. |
| 177 | + ``/if/this/is/a/path`` then the ``content_path`` |
| 178 | + represents ``/if/this/is/a``; |
| 179 | +* Each element under ``content_path`` represents a section of the URL; |
| 180 | +* The first element ``blog_path`` uses a *provider* which *specifies* a |
| 181 | + path. If that path exists then it will do nothing; |
| 182 | +* The second element uses the ``content_datetime`` provider, which will |
| 183 | + use a ``DateTime`` object returned from the specified method on the |
| 184 | + content object (the ``Post``) and create a path from it, e.g. |
| 185 | + ``2013/10/13``; |
| 186 | +* The ``content_name`` key represents the last part of the path, e.g. ``path`` |
| 187 | + from ``/if/this/is/a/path``. |
| 188 | + |
| 189 | +Now you will need to include this configuration: |
| 190 | + |
| 191 | +.. configuration-block:: |
| 192 | + |
| 193 | + .. code-block:: yaml |
| 194 | +
|
| 195 | + # src/Acme/BasicCmsBundle/Resources/config/config.yml |
| 196 | + imports: |
| 197 | + - { resource: routing_auto.yml } |
| 198 | +
|
| 199 | + .. code-block:: xml |
| 200 | +
|
| 201 | + <!-- src/Acme/BasicCmsBUndle/Resources/config/config.yml --> |
| 202 | + <?xml version="1.0" encoding="UTF-8" ?> |
| 203 | + <container |
| 204 | + xmlns="http://symfony.com/schema/dic/services" |
| 205 | + xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" |
| 206 | + xsi:schemaLocation="http://symfony.com/schema/dic/services |
| 207 | + http://symfony.com/schema/dic/services/services-1.0.xsd"> |
| 208 | +
|
| 209 | + <import resource="routing_auto.yml"/> |
| 210 | + </container> |
| 211 | + |
| 212 | + .. code-block:: php |
| 213 | +
|
| 214 | + // src/Acme/BasicCmsBundle/Resources/config/config.php |
| 215 | +
|
| 216 | + // ... |
| 217 | + $this->import('routing_auto.yml'); |
| 218 | +
|
| 219 | +and reload the fixtures: |
| 220 | + |
| 221 | +.. code-block:: bash |
| 222 | +
|
| 223 | + $ php app/console doctrine:phpcr:fixtures:load |
| 224 | +
|
| 225 | +Have a look at what you have: |
| 226 | + |
| 227 | +.. code-block:: bash |
| 228 | +
|
| 229 | + $ php app/console doctrine:phpcr:node:dump |
| 230 | + ROOT: |
| 231 | + cms: |
| 232 | + pages: |
| 233 | + Home: |
| 234 | + routes: |
| 235 | + page: |
| 236 | + home: |
| 237 | + post: |
| 238 | + 2013: |
| 239 | + 10: |
| 240 | + 12: |
| 241 | + my-first-post: |
| 242 | + my-second-post: |
| 243 | + my-third-post: |
| 244 | + my-forth-post: |
| 245 | + posts: |
| 246 | + My First Post: |
| 247 | + My Second Post: |
| 248 | + My Third Post: |
| 249 | + My Forth Post: |
| 250 | +
|
| 251 | +The routes have been automatically created! |
| 252 | + |
| 253 | +.. _`routingautobundle documentation`: http://symfony.com/doc/current/cmf/bundles/routing_auto.html |
| 254 | +.. _`SonataDoctrinePhpcrAdminBundle`: https://github.com/sonata-project/SonataDoctrinePhpcrAdminBundle |
| 255 | +.. _`routingbundle`: http://symfony.com/doc/master/cmf/bundles/routing/index.html |
0 commit comments