Named routing in Symfony 6 using controller from internal directory
P粉511749537
P粉511749537 2024-03-26 16:27:48
0
1
453

I moved all controllers to /src/Web/Controller in the Symfony 6 project as shown below

├── src
│   ├── ...
│   └── Web
│   |    ├── Controller
│   |    ├── ....
|   |── Kernel.php

Myroutes.yamlModify accordingly

#routes.yaml
controllers:
    resource: ../src/Web/Controller/
    type: annotation

Now the problem is that all my routes have a name prefixed with app_web. I guess it's due to this structure.

$ php bin/console debug:router The command output is as follows:

...
...
app_web_post_index    GET|HEAD        ANY      ANY    /post/             
app_web_post_create   GET|HEAD|POST   ANY      ANY    /post/create

Here I only want the name to be post_ind

P粉511749537
P粉511749537

reply all(1)
P粉011684326

If you don't name the routes explicitly, they will be named for you.

The

name is generated using the fully qualified names of the controller's classes and methods (although for some reason there is no last part of the namespace - controller).

If you don't want to use the automatically generated name, just name your route yourself:

#[Route('/post')]
class Post
{
    #[Route(path: '/', name: 'post_index', methods: ['HEAD', 'GET'])]
    public function index(Request $request): Response
    {
        return new Response('post index');
    }

    #[Route(path: '/create', name: 'post_create', methods: ['HEAD', 'GET', 'POST'])]
    public function create(Request $request): Response
    {
        return new Response('post create');
    }
}

Use �%8

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template