Cover of the book Symfony 5: The Fast Track

Symfony 5: The Fast Track is the best book to learn modern Symfony development, from zero to production. +300 pages in full color showing how to combine Symfony with Docker, APIs, queues & async tasks, Webpack, Single-Page Applications, etc.

Buy printed version

New in Symfony 4.1: Prefix imported route names

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>
Help the Symfony project!

As with any Open-Source project, contributing code or documentation is the most common way to help, but we also have a wide range of sponsoring opportunities.

Comments

Great news ;)
nice!
Many thanks :) :)
++1 great !!
Nice :))
Good luck ever finding any of those routes back when using text searches.
Awesome news. We're wait for it from SymfonyCon

Comments are closed.

To ensure that comments stay relevant, they are closed for old posts.