wiki/content/20201109142351-prefix_all_controller_route_names.md

1.5 KiB

date id title
2020-11-09 0fae8bc3-e520-4515-b3ca-7d8b1003d132 Prefix all controller route names

Introduction

From Symfony 4.0 the class of a controller can define the @Route annotation to set a common prefix for the URLs used by the action methods:

Syntax

use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;

/**
 * @Route("/blog")
 */
class BlogController extends Controller
{
    /**
     * @Route("/", defaults={"page": "1"}, name="blog_index")
     * @Route("/page/{page}", name="blog_index_paginated")
     */
    public function indexAction($page, $_format) { ... }

    /**
     * @Route("/posts/{slug}", name="blog_post")
     */
    public function showAction(Post $post) { ... }
}

Add a name property to the @Route annotation of the controller class and that will be considered the prefix of all route names. The following is equivalent to the previous example:

use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;

/**
 * @Route("/blog", name="blog_")
 */
class BlogController extends Controller
{
    /**
     * @Route("/", defaults={"page": "1"}, name="index")
     * @Route("/page/{page}", name="index_paginated")
     */
    public function indexAction($page, $_format) { ... }

    /**
     * @Route("/posts/{slug}", name="post")
     */
    public function showAction(Post $post) { ... }
}

nameprefix option

In Symfony 4.1 a new nameprefix option was added.