How to Include External Routing Resources

Version: 3.0
Edit this page

Warning: You are browsing the documentation for Symfony 3.0, which is no longer maintained.

Read the updated version of this page for Symfony 5.3 (the current stable version).

How to Include External Routing Resources

All routes are loaded via a single configuration file - usually app/config/routing.yml (see Routing). However, if you use routing annotations, you'll need to point the router to the controllers with the annotations. This can be done by "importing" directories into the routing configuration:

  • YAML
  • XML
  • PHP
1
2
3
4
# app/config/routing.yml
app:
    resource: '@AppBundle/Controller/'
    type:     annotation # required to enable the Annotation reader for this resource

Note

When importing resources from YAML, the key (e.g. app) is meaningless. Just be sure that it's unique so no other lines override it.

The resource key loads the given routing resource. In this example the resource is a directory, where the @AppBundle shortcut syntax resolves to the full path of the AppBundle. When pointing to a directory, all files in that directory are parsed and put into the routing.

Note

You can also include other routing configuration files, this is often used to import the routing of third party bundles:

  • YAML
  • XML
  • PHP
1
2
3
# app/config/routing.yml
app:
    resource: '@AcmeOtherBundle/Resources/config/routing.yml'

Prefixing Imported Routes

You can also choose to provide a "prefix" for the imported routes. For example, suppose you want to prefix all routes in the AppBundle with /site (e.g. /site/blog/{slug} instead of /blog/{slug}):

  • YAML
  • XML
  • PHP
1
2
3
4
5
# app/config/routing.yml
app:
    resource: '@AppBundle/Controller/'
    type:     annotation
    prefix:   /site

The path of each route being loaded from the new routing resource will now be prefixed with the string /site.

Adding a Host Requirement to Imported Routes

You can set the host regex on imported routes. For more information, see How to Match a Route Based on the Host.

This work, including the code samples, is licensed under a Creative Commons BY-SA 3.0 license.