在尝试匹配像 site.com/photos
和 这样的 url 之后,我想在
。我正在使用注释并有两个 Controller 。site.com/{username}
末尾有一个带通配符的 url >site.com/blog
我在这里找到了答案Ordering of routes using annotations
但是文件夹结构在版本 4 中有所不同,并且在我使用注释时他们使用 YAML 的答案中,所以我不完全理解将什么放在哪里。
视频 Controller
/**
* @Route("/videos", name="videos")
*/
public function index()
{
// Show video homepage
}
用户 Controller
/**
* @Route("/{username}", name="profile")
*/
public function index()
{
// Hello {username} !
}
按此顺序,site.com/videos
到达用户 Controller 而不是视频。我是否必须切换到手动将 所有 URL 结构放入 yaml 格式,或者有没有办法在注释中设置优先级?
到目前为止,我找到的唯一方法是创建一个以字母“Z”开头的 Controller ,并将 Action /路线放在那里,这似乎是最后运行路线。
最佳答案
您链接到的问题实际上非常相关。你问的和另一个人一样:
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
可能是这样的:
controllers:
resource: ../../src/Controller/
type: annotation
你可以用这个替换上面的 YAML——注意顺序很重要:
Video:
resource: App\Controller\VideoController
type: annotation
User:
resource: App\Controller\UserController
type: annotation
然后 site.com/videos
首先到达视频 Controller 。
关于php - 路由顺序 symfony 4,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48494301/