[英]Symfony create routing for mydomain.com/parameter
I need to create a routing rule in Symfony to receive a parameter as the first value after the domain.我需要在 Symfony 中创建一个路由规则来接收一个参数作为域之后的第一个值。
I mean, www.mydomain.com/myusername我的意思是,www.mydomain.com/myusername
Username is a variable to show the profile of an user.用户名是一个变量,用于显示用户的个人资料。
Is it possible that in some cases, Symfony identify it needs to consider this as a variable (dynamically) and in other cases it needs to follow the normal case with /controller/action?是否有可能在某些情况下,Symfony 确定它需要将其视为一个变量(动态),而在其他情况下它需要遵循正常情况下的 /controller/action?
I'm not entirely sure what version you're referring to but it's possible to have such conflicting routing in Symfony 2. What you should do is add your route at the very bottom of app/config/routing.yml
.我不完全确定您指的是哪个版本,但在 Symfony 2 中可能存在这种冲突的路由。您应该做的是在
app/config/routing.yml
最底部添加您的路由。 The order is important, because in Symfony the first matched route always wins as stated in the docs顺序很重要,因为在 Symfony 中,第一个匹配的路由总是如文档中所述
The Symfony router will always choose the first matching route it finds.
Symfony 路由器将始终选择它找到的第一个匹配路由。
So, by having all of your other routes come before the matching route for domain.com/*
everything else should be working.因此,通过让所有其他路由出现在
domain.com/*
的匹配路由之前,其他一切都应该正常工作。
I must say, though, this isn't considered a best practice because it's usually harder to maintain and debug.不过,我必须说,这不被认为是最佳实践,因为它通常更难维护和调试。 Furthermore, if you have a lot of routes it would take considerably longer time for your requests to complete, because it would go though all routes until it matches the last route.
此外,如果您有很多路线,完成您的请求将花费相当长的时间,因为它会遍历所有路线,直到与最后一条路线匹配。
Add your parameter as an optional one like:添加您的参数作为可选参数,例如:
public functionName($parameter = null)
{
if($parameter = null)
{ //render regular view }
else
{ //render user profile view}
}
Hope i got your question right Cheers希望我答对了你的问题 干杯
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.