|
| 1 | +.. index:: |
| 2 | + single: Routing; Importing routing resources |
| 3 | + |
| 4 | +How to Include External Routing Resources |
| 5 | +========================================= |
| 6 | + |
| 7 | +All routes are loaded via a single configuration file - usually ``app/config/routing.yml`` |
| 8 | +(see `Creating Routes`_ above). However, if you use routing annotations, |
| 9 | +you'll need to point the router to the controllers with the annotations. |
| 10 | +This can be done by "importing" directories into the routing configuration: |
| 11 | + |
| 12 | +.. configuration-block:: |
| 13 | + |
| 14 | + .. code-block:: yaml |
| 15 | +
|
| 16 | + # app/config/routing.yml |
| 17 | + app: |
| 18 | + resource: '@AppBundle/Controller/' |
| 19 | + type: annotation # required to enable the Annotation reader for this resource |
| 20 | +
|
| 21 | + .. code-block:: xml |
| 22 | +
|
| 23 | + <!-- app/config/routing.xml --> |
| 24 | + <?xml version="1.0" encoding="UTF-8" ?> |
| 25 | + <routes xmlns="http://symfony.com/schema/routing" |
| 26 | + xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" |
| 27 | + xsi:schemaLocation="http://symfony.com/schema/routing |
| 28 | + http://symfony.com/schema/routing/routing-1.0.xsd"> |
| 29 | +
|
| 30 | + <!-- the type is required to enable the annotation reader for this resource --> |
| 31 | + <import resource="@AppBundle/Controller/" type="annotation"/> |
| 32 | + </routes> |
| 33 | +
|
| 34 | + .. code-block:: php |
| 35 | +
|
| 36 | + // app/config/routing.php |
| 37 | + use Symfony\Component\Routing\RouteCollection; |
| 38 | +
|
| 39 | + $collection = new RouteCollection(); |
| 40 | + $collection->addCollection( |
| 41 | + // second argument is the type, which is required to enable |
| 42 | + // the annotation reader for this resource |
| 43 | + $loader->import("@AppBundle/Controller/", "annotation") |
| 44 | + ); |
| 45 | +
|
| 46 | + return $collection; |
| 47 | +
|
| 48 | +.. note:: |
| 49 | + |
| 50 | + When importing resources from YAML, the key (e.g. ``app``) is meaningless. |
| 51 | + Just be sure that it's unique so no other lines override it. |
| 52 | + |
| 53 | +The ``resource`` key loads the given routing resource. In this example the |
| 54 | +resource is a directory, where the ``@AppBundle`` shortcut syntax resolves |
| 55 | +to the full path of the AppBundle. When pointing to a directory, all files |
| 56 | +in that directory are parsed and put into the routing. |
| 57 | + |
| 58 | +.. note:: |
| 59 | + |
| 60 | + You can also include other routing configuration files, this is often |
| 61 | + used to import the routing of third party bundles: |
| 62 | + |
| 63 | + .. configuration-block:: |
| 64 | + |
| 65 | + .. code-block:: yaml |
| 66 | +
|
| 67 | + # app/config/routing.yml |
| 68 | + app: |
| 69 | + resource: '@AcmeOtherBundle/Resources/config/routing.yml' |
| 70 | +
|
| 71 | + .. code-block:: xml |
| 72 | +
|
| 73 | + <!-- app/config/routing.xml --> |
| 74 | + <?xml version="1.0" encoding="UTF-8" ?> |
| 75 | + <routes xmlns="http://symfony.com/schema/routing" |
| 76 | + xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" |
| 77 | + xsi:schemaLocation="http://symfony.com/schema/routing |
| 78 | + http://symfony.com/schema/routing/routing-1.0.xsd"> |
| 79 | +
|
| 80 | + <import resource="@AcmeOtherBundle/Resources/config/routing.xml" /> |
| 81 | + </routes> |
| 82 | +
|
| 83 | + .. code-block:: php |
| 84 | +
|
| 85 | + // app/config/routing.php |
| 86 | + use Symfony\Component\Routing\RouteCollection; |
| 87 | +
|
| 88 | + $collection = new RouteCollection(); |
| 89 | + $collection->addCollection( |
| 90 | + $loader->import("@AcmeOtherBundle/Resources/config/routing.php") |
| 91 | + ); |
| 92 | +
|
| 93 | + return $collection; |
| 94 | +
|
| 95 | +Prefixing Imported Routes |
| 96 | +~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 97 | + |
| 98 | +You can also choose to provide a "prefix" for the imported routes. For example, |
| 99 | +suppose you want to prefix all routes in the AppBundle with ``/site`` (e.g. |
| 100 | +``/site/blog/{slug}`` instead of ``/blog/{slug}``): |
| 101 | + |
| 102 | +.. configuration-block:: |
| 103 | + |
| 104 | + .. code-block:: yaml |
| 105 | +
|
| 106 | + # app/config/routing.yml |
| 107 | + app: |
| 108 | + resource: '@AppBundle/Controller/' |
| 109 | + type: annotation |
| 110 | + prefix: /site |
| 111 | +
|
| 112 | + .. code-block:: xml |
| 113 | +
|
| 114 | + <!-- app/config/routing.xml --> |
| 115 | + <?xml version="1.0" encoding="UTF-8" ?> |
| 116 | + <routes xmlns="http://symfony.com/schema/routing" |
| 117 | + xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" |
| 118 | + xsi:schemaLocation="http://symfony.com/schema/routing |
| 119 | + http://symfony.com/schema/routing/routing-1.0.xsd"> |
| 120 | +
|
| 121 | + <import |
| 122 | + resource="@AppBundle/Controller/" |
| 123 | + type="annotation" |
| 124 | + prefix="/site" /> |
| 125 | + </routes> |
| 126 | +
|
| 127 | + .. code-block:: php |
| 128 | +
|
| 129 | + // app/config/routing.php |
| 130 | + use Symfony\Component\Routing\RouteCollection; |
| 131 | +
|
| 132 | + $app = $loader->import('@AppBundle/Controller/', 'annotation'); |
| 133 | + $app->addPrefix('/site'); |
| 134 | +
|
| 135 | + $collection = new RouteCollection(); |
| 136 | + $collection->addCollection($app); |
| 137 | +
|
| 138 | + return $collection; |
| 139 | +
|
| 140 | +The path of each route being loaded from the new routing resource will now |
| 141 | +be prefixed with the string ``/site``. |
| 142 | + |
| 143 | +Adding a Host Requirement to Imported Routes |
| 144 | +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 145 | + |
| 146 | +You can set the host regex on imported routes. For more information, see |
| 147 | +:ref:`component-routing-host-imported`. |
0 commit comments