Samuel Roze
Contributed by Samuel Roze in #25178

In Symfony 3.4 and 4.0 we added the possibility of prefixing the names of all the routes defined in a controller class with the name option in the main @Route annotation. In the following example, the route names will be blog_index and blog_post:

1
2
3
4
5
6
7
8
9
10
11
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;

/** @Route("/blog", name="blog_") */
class BlogController extends Controller
{
    /** @Route("/", name="index") */
    public function indexAction() { ... }

    /** @Route("/posts/{slug}", name="post") */
    public function showAction(Post $post) { ... }
}

In Symfony 4.1 we improved this feature adding a new name_prefix option to prefix the names of the routes imported in configuration files. This will allow for example to import a given file multiple times and also tweak the route names of some third-party library/bundle:

1
2
3
4
5
6
7
8
9
app:
    resource: ../controller/routing.yaml

api:
    resource: ../controller/routing.yaml
    # this prefix is added to all the action route names
    name_prefix: api_
    # this prefix is added to all the action URLs
    prefix: /api

This is how the above config would look when using the XML format:

1
2
3
4
5
6
7
8
9
10
<?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="../controller/routing.xml" />
    <import resource="../controller/routing.xml" prefix="/api" name-prefix="api_" />

</routes>
Published in #Living on the edge