New in Symfony 4.3: Simpler Routing Options Config
March 28, 2019 • Published by Javier Eguiluz
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
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
- YAML
- XML
1 2 3 4 5 6
// src/Controller/BlogController.php
/**
* @Route("/category/{name}", name="category", format="json", locale="fr")
*/
public function category()
1 2 3 4 5
# config/routes.yaml
category:
path: /category/{name}
locale: fr
format: json
1 2 3 4 5 6 7 8 9 10 11 12
<!-- config/routes.xml -->
<?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
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>
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 are closed.
To ensure that comments stay relevant, they are closed for old posts.