Upcoming Official Symfony Conferences
WARNING: You are browsing the documentation for Symfony 3.3 which is not maintained anymore. Consider upgrading your projects to Symfony 4.1.

How to Include External Routing Resources

3.3 version

How to Include External Routing Resources

Simple applications can define all their routes in a single configuration file - usually app/config/routing.yml (see Loading Routes). However, in most applications it's common to import routes definitions from different resources: PHP annotations in controller files, YAML or XML files stored in some directory, etc.

This can be done by importing routing resources from the main routing file:

  • YAML
     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    # app/config/routing.yml
    app_file:
        # loads routes from the given routing file stored in some bundle
        resource: '@AcmeOtherBundle/Resources/config/routing.yml'
    
    app_annotations:
        # loads routes from the PHP annotations of the controllers found in that directory
        resource: '@AppBundle/Controller/'
        type:     annotation
    
    app_directory:
        # loads routes from the YAML or XML files found in that directory
        resource: '../legacy/routing/'
        type:     directory
    
    app_bundle:
        # loads routes from the YAML or XML files found in some bundle directory
        resource: '@AppBundle/Resources/config/routing/public/'
        type:     directory
    
  • XML
     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    <!-- app/config/routing.xml -->
    <?xml version="1.0" encoding="UTF-8" ?>
    <routes xmlns="http://symfony.com/schema/routing"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://symfony.com/schema/routing
            http://symfony.com/schema/routing/routing-1.0.xsd">
    
        <!-- loads routes from the given routing file stored in some bundle -->
        <import resource="@AcmeOtherBundle/Resources/config/routing.yml" />
    
        <!-- loads routes from the PHP annotations of the controllers found in that directory -->
        <import resource="@AppBundle/Controller/" type="annotation" />
    
        <!-- loads routes from the YAML or XML files found in that directory -->
        <import resource="../legacy/routing/" type="directory" />
    
        <!-- loads routes from the YAML or XML files found in some bundle directory -->
        <import resource="@AppBundle/Resources/config/routing/public/" type="directory" />
    </routes>
    
  • PHP
     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    // app/config/routing.php
    use Symfony\Component\Routing\RouteCollection;
    
    $collection = new RouteCollection();
    $collection->addCollection(
        // loads routes from the given routing file stored in some bundle
        $loader->import("@AcmeOtherBundle/Resources/config/routing.yml")
    
        // loads routes from the PHP annotations of the controllers found in that directory
        $loader->import("@AppBundle/Controller/", "annotation")
    
        // loads routes from the YAML or XML files found in that directory
        $loader->import("../legacy/routing/", "directory")
    
        // loads routes from the YAML or XML files found in some bundle directory
        $loader->import("@AppBundle/Resources/config/routing/public/", "directory")
    );
    
    return $collection;
    

Note

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

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
    1
    2
    3
    4
    5
    # app/config/routing.yml
    app:
        resource: '@AppBundle/Controller/'
        type:     annotation
        prefix:   /site
    
  • XML
     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    <!-- app/config/routing.xml -->
    <?xml version="1.0" encoding="UTF-8" ?>
    <routes xmlns="http://symfony.com/schema/routing"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://symfony.com/schema/routing
            http://symfony.com/schema/routing/routing-1.0.xsd">
    
        <import
            resource="@AppBundle/Controller/"
            type="annotation"
            prefix="/site" />
    </routes>
    
  • PHP
     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    // app/config/routing.php
    use Symfony\Component\Routing\RouteCollection;
    
    $app = $loader->import('@AppBundle/Controller/', 'annotation');
    $app->addPrefix('/site');
    
    $collection = new RouteCollection();
    $collection->addCollection($app);
    
    return $collection;
    

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 Using Host Matching of Imported Routes.

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