简体   繁体   English

Symfony2控制器路由冲突

[英]Symfony2 Controller Route clash

Hey all I have a bit of a problem with root annotations in Symfony2. 嘿,我在Symfony2中使用根注释存在一些问题。

I have two different controllers that call methods from the same URL positions /test. 我有两个不同的控制器,它们从相同的URL位置/ test调用方法。

Controller 1: 控制器1:

    **
     * @Route("/test", service="myProject.test.controller.art")
     * @Cache(expires="+5 minutes", public=true)
     */
    class BlogController
    {

      /**
         * @Route("/{text}", defaults={"text" = null})
         * @Route("/topic/{tag}", defaults={"tag" = null})
         * @Method({"GET"})

         */
        public function listAction(ArtQuery $query)
        {
           //.................
        }
    }

Controller 2: 控制器2:

**
 * @Route("/test" , service="myProject.test.controller.sitemap"))
 * @Cache(expires="+5 minutes", public=true)
 */
class SitemapController
{
/**
     * @Route("/sitemap.xml/")
     * @Method({"GET"})
     */
    public function sitemapAction()
    {
       //..................
    }
}

The problem is that the second Controller is never matched only if is add in my @route("/sitemap.xml/") but I realy want the route to be only @route("/sitemap.xml") . 问题是,只有在我的@route("/sitemap.xml/")添加第二个Controller时,它才永远不会匹配,但我真的希望路由仅是@route("/sitemap.xml")

I think the problem is when i input the url /test/sitemap.xml Symfony treats sitemap.xml as /{text} variable route in first controller. 我认为问题是当我输入URL /test/sitemap.xml Symfony在第一个控制器中将sitemap.xml视为/ {text}变量路由时。

Can I make a exception so that first controller ends as soon as it hits sitemap.xml....? 我可以做一个例外,以便第一个控制器在击中sitemap.xml时就结束了吗?

I read something about requirements but dont quiet understand this concept 我读了一些有关需求的内容,但并没有安静地理解这个概念

according to documentation 根据文件

Earlier Routes always Win 较早的路线总能取胜

What this all means is that the order of the routes is very important. 这一切都意味着路线的顺序非常重要。 If the blog_show route were placed above the blog route, the URL /blog/2 would match blog_show instead of blog since the {slug} parameter of blog_show has no requirements. 如果将blog_show路由置于博客路由上方,则URL / blog / 2将匹配blog_show而不是Blog,因为blog_show的{slug}参数没有要求。 By using proper ordering and clever requirements, you can accomplish just about anything. 通过正确的订购和明智的要求,您几乎可以完成任何事情。

http://symfony.com/doc/current/book/routing.html http://symfony.com/doc/current/book/routing.html

i suggest to use yml or xml file for routing or you can make a requirement in your first route 我建议使用yml或xml文件进行路由,或者您可以在第一个路由中提出要求

   /**
     * @Route("/{text}", defaults={"text" = null}, requirements={"text" = "^(?!sitemap\.xml)$"})
     * @Route("/topic/{tag}", defaults={"tag" = null})
     * @Method({"GET"})

     */
    public function listAction(ArtQuery $query)
    {
       //.................
    }

The router will use the first route that matches the path. 路由器将使用与路径匹配的第一条路由。

The only way to prioritize a route over another which could match is to ensure that the stricter requirements are check before the weaker ones. 优先于可能匹配的路由优先的唯一方法是确保先检查较严格的要求,然后再检查较弱的要求。

Normally this would be accomplished by placing the sitemapAction method above listAction . 通常,这可以通过将sitemapAction方法放在listAction However since you have a controller for each of these, you will have to put the controllers in the correct order. 但是,由于每个控制器都有一个控制器,因此必须按正确的顺序放置控制器。

To do this you will need to add the controllers to the config individually like this: 为此,您需要将控制器分别添加到配置中,如下所示:

app_sitemap:
    resource: "@AppBundle/Controller/SitemapController.php"
    type:     annotation
    prefix:   /


app_blog:
    resource: "@AppBundle/Controller/BlogController.php"
    type:     annotation
    prefix:   /

This way the controllers will be iterated in this order. 这样,将按此顺序迭代控制器。

However it is better if you can give each route a unique path, perhaps: 但是,最好为每个路径指定唯一的路径,也许:

@Route("/query/{text}", defaults={"text" = null})

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

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