Symfony News

New in Symfony 4.1: Configurable trailing slash on imported routes

Nicolas Grekas

Contributed by
Nicolas Grekas
in #26284.

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

Be trained by Symfony experts - 2018-05-22 Clichy - 2018-05-22 Paris - 2018-05-22 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