Symfony 4 global route prefix

拜拜、爱过 提交于 2019-11-29 12:29:51

This works with annotations, giving every controller 'this/prefix' :

# file: config/routes/annotations.yaml

controllers:
    resource: ../../src/Controller/
    type: annotation
    prefix: this/prefix

Then for imported routes 'this/prefix' gets applied to all of FOSUserBundle's routes below (yes I tested it).

# file: config/routes.yaml

fos_user:
    resource: "@FOSUserBundle/Resources/config/routing/all.xml"
    prefix: this/prefix

If you are not using annotations you should be able to import your separate routing file (which can import many more) applying the prefix to everything in the resource. But drop the "@" notation and use relative path to your file. The value of 'this/prefix' can be configured as a Service Container Parameter, look here: http://symfony.com/doc/current/routing/service_container_parameters.html

I'm probably not understanding the question because I don't know why you would want a global prefix in your app. You can set a prefix when importing a route file even if you are using yaml and no bundles.

# config/routes.yaml
blog:
    resource: '../src/Resources/config/routes/blog.yaml'
    prefix: blog

# src/Resources/config/routes/blog.yaml
blog_show:
    path: /show
    controller: App\Controller\BlogController::show

blog_list:
    path: /list
    controller: App\Controller\BlogController::list

bin/console debug:router would yield

blog_show          ANY      ANY      ANY    /blog/show     
blog_list          ANY      ANY      ANY    /blog/list  

But again, I suspect you are asking for something else. Perhaps you could add an example to your question?

I have the same issue and haven't been able to solve it using yaml configuration but I have it working adding an annotation on the top of the class like this:

/**
 *
 * Rest\Route("/api")
 */
class TestController extends FosRestController
{
   /**
     * @Rest\Route("/test", name="tm_test")
     * @Method({"GET"})
     */
    public function testAction()
    {
        ...
    }

}

That will produce a route /api/test for the testAction()

Look this post: https://symfony.com/blog/new-in-symfony-3-4-prefix-all-controller-route-names

Hope it helps

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!