Symfony 4全局路由前缀 [英] Symfony 4 global route prefix

查看:92
本文介绍了Symfony 4全局路由前缀的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Symfony 4应用程序中找不到有关全局路由前缀的任何信息.唯一的之类发现使用@route注释了控制器.但是我不使用注释,我需要所有控制器具有相同的前缀.

I can't find any info about the global route prefix in a Symfony 4 application. The only thing I've found is annotating the controllers with @route. But I don't use annotations and I need all the controllers to have the same prefix.

现在我可以在S3中的app/config/routing.yml文件中这样做:

Now I could do that in S3 like this in the app/config/routing.yml file:

app:
    resource: '@AppBundle/Controller/'
    type: annotation
    prefix: /foo

但是S4是无捆绑软件的,我不能做同样的事情-我将不得不创建一个不需要的捆绑软件.

But S4 is bundleless and I can't do the same - I would have to create a bundle which I don't need.

是否有可能在Symfony 4中完全定义一个全局路由前缀,或者我最终会给每个根目录加上前缀/创建自定义路由加载器,尤其是如果我在YAML中配置路由时?

Is it possible to define a global route prefix in Symfony 4 at all or I'm going to end up with prefixing every single root/creating a custom route loader, especially if I configure routes in YAML?

推荐答案

这对带有注释有效,为每个控制器提供了"this/prefix":

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

# file: config/routes/annotations.yaml

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

然后将导入的路由"this/prefix"应用于以下所有FOSUserBundle的路由(是的,我已经对其进行了测试).

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

如果您不使用注释,则应该可以将单独的路由文件(可以导入更多文件)导入,并将前缀应用于资源中的所有内容.但是,请删除"@"表示法,并使用文件的相对路径. 'this/prefix'的值可以配置为服务容器参数,请看此处: http://symfony.com/doc/current/routing/service_container_parameters.html

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

这篇关于Symfony 4全局路由前缀的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆