路由顺序 symfony 4 [英] Routing order symfony 4
问题描述
在尝试匹配 site.com/photos
和 等 url 后,我想在
.我正在使用注释并有两个控制器.site.com/{username}
末尾有一个带通配符的 url>site.com/blog
I want to have a url with a wildcard at the end site.com/{username}
after trying to match url's like site.com/photos
and site.com/blog
. I'm using annotations and have two controllers.
我在这里找到了答案使用注释的路由排序
但是文件夹结构在第 4 版中有所不同,在我使用注释时他们使用的是 YAML,所以我不完全明白该放什么.
But the folder structure is different in version 4 and in the answer they are using YAML while I'm using annotations, so I don't fully understand where to put what.
视频控制器
/**
* @Route("/videos", name="videos")
*/
public function index()
{
// Show video homepage
}
用户控制器
/**
* @Route("/{username}", name="profile")
*/
public function index()
{
// Hello {username} !
}
按此顺序,site.com/videos
到达用户控制器而不是视频.我是否必须切换到手动将 all URL 结构置于 yaml 格式中,或者有没有办法在注释中设置优先级?
In this order, site.com/videos
reaches User controller instead of videos. Do I have to switch to manually putting all URL structures in yaml format or is there a way to set priority in annotations?
到目前为止,我发现的唯一方法是创建一个以字母Z"开头的控制器,并将动作/路线放在那里,这似乎是最后运行路线.
So far the only method I found was to create a controller starting with the letter "Z", and putting the action/route in there, that seems to run the route last.
推荐答案
您链接的问题实际上非常相关.你问的和其他人一样:
The question you linked to is actually very relevant. You are asking just the same as the other guy:
但是如果你在控制器中将路由定义为注解,你怎么能做到这一点呢?在这种情况下,有没有办法指定这条路线的顺序?
But how can you do this if you define your routes as annotations in your controllers? Is there any way to specify the ordering of this routes in this case?
在 Symfony 4 中,你的 config/routes.yaml
可能是空的,config/routes/annotations.yaml
可能是这样的:
In Symfony 4, your config/routes.yaml
is probably empty and config/routes/annotations.yaml
probably like this:
controllers:
resource: ../../src/Controller/
type: annotation
你可以用这个 – 替换上面的 YAML;请注意顺序确实很重要:
You can replace the above YAML with this – notice that the order does matter:
Video:
resource: App\Controller\VideoController
type: annotation
User:
resource: App\Controller\UserController
type: annotation
然后 site.com/videos
首先到达视频控制器.
Then site.com/videos
reaches the video controller first.
这篇关于路由顺序 symfony 4的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!