Symfony News

New in Symfony 4.3: Simpler Routing Options Config

Jules Pietri

Contributed by
Jules Pietri
in #30508.

The Routing component maps HTTP requests to PHP code using some configuration options defined for each of the application routes. In Symfony 4.3 we made some improvements to configure some of those options more easily.

First, we've exposed the Unicode routing support (added back in Symfony 3.2) via a new utf8 option, so you don't have to configure it through the generic options metadata. The following example shows the difference when using each of the supported formats to configure routes:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
// PHP Annotations

/**
-  * @Route("/category/{name}", name="category", options={"utf8": true})
+  * @Route("/category/{name}", name="category", utf8=true)
 */
public function category()

// YAML

category:
    path:     /category/{name}
    controller: App\Controller\DefaultController::category
-     options:
-         utf8: true
+     utf8: true

// XML

- <route id="category" path="/category/{name}"
-        controller="App\Controller\DefaultController::category">
-     <option key="utf8">true</option>
- </route>
+ <route id="category" path="/category/{name}"
+        controller="App\Controller\DefaultController::category"
+        utf8="true" />

In addition, we've added two new route options called locale and format to configure the default value of the special routing parameters _locale and _format:

  • Annotations
    1
    2
    3
    4
    5
    6
    // src/Controller/BlogController.php
    
    /**
     * @Route("/category/{name}", name="category", format="json", locale="fr")
     */
    public function category()
    
  • YAML
    1
    2
    3
    4
    5
    # config/routes.yaml
    category:
      path:   /category/{name}
      locale: fr
      format: json
    
  • XML
     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    <!-- config/routes.xml -->
    <?xml version="1.0" encoding="UTF-8" ?>
    <routes xmlns="https://symfony.com/schema/routing"
        xmlns:xsi="https://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="https://symfony.com/schema/routing
            https://symfony.com/schema/routing/routing-1.0.xsd">
    
        <route id="category" path="/category/{name}"
               controller="App\Controller\BlogController::category"
               format="json" locale="fr" />
    
    </routes>
    

Be trained by Symfony experts - 2019-04-1 Clichy - 2019-04-4 Clichy - 2019-04-10 Clichy


About us

What a Symfony developer should know about the framework: News, Jobs, Tweets, Events, Videos,...

Resources

Find us on Twitter

Find us on Facebook