简体   繁体   English

Symfony 4 全局路由前缀

[英]Symfony 4 global route prefix

I can't find any info about the global route prefix in a Symfony 4 application.我在 Symfony 4 应用程序中找不到有关全局路由前缀的任何信息。 The only thing I've found is annotating the controllers with @route .我发现的唯一一件事是用@route注释控制器。 But I don't use annotations and I need all the controllers to have the same prefix.但是我不使用注释,我需要所有控制器都具有相同的前缀。

Now I could do that in S3 like this in the app/config/routing.yml file:现在我可以在 S3 中的app/config/routing.yml文件中这样做:

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

But S4 is bundleless and I can't do the same - I would have to create a bundle which I don't need.但是 S4 是无捆绑的,我不能这样做 - 我必须创建一个我不需要的捆绑包。

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?是否可以在 Symfony 4 中定义全局路由前缀,或者我最终会为每个根添加前缀/创建自定义路由加载器,特别是如果我在 YAML 中配置路由?

This works with annotations, giving every controller 'this/prefix' :这适用注释,为每个控制器提供“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).然后对于导入的路由,'this/prefix' 被应用于下面所有 FOSUserBundle 的路由(是的,我测试了它)。

# 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 'this/prefix' 的值可以配置为服务容器参数,请看这里: 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.即使您使用的是 yaml 并且没有包,您也可以在导入路由文件时设置前缀。

# 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 bin/控制台调试:路由器会产生

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?也许您可以为您的问题添加一个示例?

There are several ways to achieve prefix to any route.有几种方法可以实现任何路由的前缀。 They differ mainly due to the route type they target.它们的不同主要在于它们所针对的路线类型。

For routes defined by annotations you need to edit config/routes/annotations.yaml and put there prefix: your-prefix key-value pair.对于由注释定义的路由,您需要编辑config/routes/annotations.yaml并将prefix: your-prefix放在那里prefix: your-prefix键值对。

For routes defined by YAML list you need to edit config/routes.yaml put there under a new route prefix: your-prefix key-value pair and resource: "routes/routes-that-you-want-to-be-prefixed.yaml" and create the file routes/routes-that-you-want-to-be-prefixed.yaml that has all the routes then remove all routes from config/routes.yaml besides that one which uses resource: "routes/routes-that-you-want-to-be-prefixed.yaml" .对于YAML 列表定义的路由,您需要编辑config/routes.yaml并将其放在新的路由prefix: your-prefix键值对和resource: "routes/routes-that-you-want-to-be-prefixed.yaml"并创建包含所有routes/routes-that-you-want-to-be-prefixed.yaml的文件routes/routes-that-you-want-to-be-prefixed.yaml config/routes.yaml然后从config/routes.yaml删除所有路由,除了使用resource: "routes/routes-that-you-want-to-be-prefixed.yaml"

You may also use quick and dirty approach if you're in hurry and want to check prefixing.如果您很着急并想检查前缀,您也可以使用快速而肮脏的方法。 Edit src/Kernel.php and suffix all $routes->import(..) with ->prefix('your-prefix) (no need to change any routing files)编辑src/Kernel.php并将所有$routes->import(..)后缀$routes->import(..) ->prefix('your-prefix) (无需更改任何路由文件)

I wrote a comprehensive description with some example code samples as an aswer to that question: Running Symfony 5 with reverse proxy in subdirectory我用一些示例代码示例写了一个全面的描述作为这个问题的答案: Running Symfony 5 with reverse proxy in subdirectory

Example code snippets have been tested against Symfony 4.4 and Symfony 5.1 and worked without any problem.示例代码片段已经针对Symfony 4.4Symfony 5.1进行了测试,并且没有任何问题。

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:我有同样的问题,无法使用 yaml 配置解决它,但我让它在类的顶部添加一个注释,如下所示:

/**
 *
 * 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()这将为testAction()生成一个路由/api/test

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

Hope it helps希望它有帮助

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM