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
Published in #Living on the edge