search

Home  >  Q&A  >  body text

Named routing in Symfony 6 using controller from internal directory

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粉511749537P粉511749537308 days ago501

reply all(1)I'll reply

  • P粉011684326

    P粉0116843262024-03-27 12:11:43

    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

    reply
    0
  • Cancelreply