Norway



Samuel Roze  - 3397cc9d55eb953351c84ed40e9425a7 - New in Symfony 4.1: Prefix imported route names

Contributed by
Samuel Roze
in #25178.

In 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 @ annotation. In the following example, the
route will be blog_index and blog_post:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
use SensioBundleFrameworkExtraBundleConfigurationRoute;

/** @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 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
:
    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>

Be trained by Symfony experts
2018-02-5 Paris
2018-02-5 Paris
2018-02-7 Paris

- uEkLumGgqcM - New in Symfony 4.1: Prefix imported route names



Source link

LEAVE A REPLY

Please enter your comment!
Please enter your name here