New in Symfony 4.1: Prefix imported route names
![Avatar of Javier Eguiluz](https://connect.symfony.com/api/images/17170f22-52c0-4adf-a75d-29cb01d53176.png?format=28x28)
Warning: This post is about an unsupported Symfony version. Some of this information may be out of date. Read the most recent Symfony Docs.
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
![Avatar of benjamin catelain](https://connect.symfony.com/api/images/e0881ba8-910f-411f-aef8-29c60383f861.png?format=48x48)
![Avatar of Benjamin Ellis](https://connect.symfony.com/api/images/e8e87d07-aca6-4fc8-af9a-02b3fc88f208.png?format=48x48)
![Avatar of Chaouch abderroauf](https://connect.symfony.com/api/images/02ea1ef6-7ae4-4d4e-9147-2cda7e1437be.png?format=48x48)
![Avatar of Kendrick](https://connect.symfony.com/api/images/c513bb4e-f044-43ac-8f7c-541843705f62.png?format=48x48)
![Avatar of Şafak Saylam](https://connect.symfony.com/api/images/9f4033c0-483a-42c8-b267-06a3586c31e0.png?format=48x48)
![Avatar of Lynn van der Berg](https://connect.symfony.com/api/images/2b84e3e4-a0b3-475c-96a3-49f3b0a96d77.png?format=48x48)
![Avatar of Alexey Samara](https://connect.symfony.com/api/images/196a4c11-8a02-42bd-a0df-767d2d0fc281.png?format=48x48)
Comments are closed.
To ensure that comments stay relevant, they are closed for old posts.