New in Symfony 4.1: Configurable trailing slash on imported routes
May 22, 2018 • 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.
In Symfony 4.1 we worked hard on the Routing component to make it the fastest PHP router, to allow to translate route paths, to make route config more concise and to allow prefix imported route names.
Another routing feature added to Symfony 4.1 is the possibility of configuring the trailing slash of the root route when importing a collection of routes. Consider the following configuration:
1 2 3 4 5 6 7
# config/routes.yaml
_api_routes:
resource: '../src/Controller/Api'
type: 'annotation'
prefix: '/api'
# ...
If any of the imported routes uses /
as its path:
1 2 3 4 5 6 7 8 9 10 11 12 13 14
use Symfony\Component\Routing\Annotation\Route;
class ApiController extends Controller
{
/**
* @Route("/", name="api_index")
*/
public function index()
{
// ...
}
// ...
}
Symfony adds the /
path to the import prefix, so the result is an /api/
path. This behavior was a well-known Symfony limitation that made it impossible
to have a root route imported under a prefix without a trailing slash. In
Symfony 4.1 we fixed this limitation introducing a new config option called
trailing_slash_on_root
:
1 2 3 4 5 6 7
# config/routes.yaml
_api_routes:
resource: '../src/Controller/Api'
type: 'annotation'
prefix: '/api'
# to maintain backward compatibility, its value is 'true' by default
trailing_slash_on_root: false
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.