简体   繁体   English

Symfony 为 mydomain.com/parameter 创建路由

[英]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.

相关问题 在Heroku上将app.mydomain.com作为mydomain.com/app运行 - Run app.mydomain.com as mydomain.com/app on Heroku 网址重写:将mydomain.com/index.php更改为mydomain.com/home - URL rewriting: Change mydomain.com/index.php to mydomain.com/home 使用PHP / Nginx将cookie域从无域迁移到.mydomain.com - Migrate cookie domain from no domain to .mydomain.com with PHP/Nginx 创建一个自定义的wordpress插件,该插件可以像mydomain.com/mytemplate.php?id=xx这样直接调用 - Create a custom wordpress plugin than can be called directly like mydomain.com/mytemplate.php?id=xx Laravel 4.1使用301永久重定向将所有请求从www.mydomain.com重定向到mydomain.com - Laravel 4.1 Redirect all requests from www.mydomain.com to mydomain.com with 301 permanent redirect 如何在Codeigniter中使用mobiledetect.net将mydomain.com重定向到m.mydomain.com? - How to redirect mydomain.com to m.mydomain.com with mobiledetect.net in codeigniter? PHP邮件未从mydomain.com发送到Google应用程序电子邮件myname@mydomain.com - PHP mail is not sending from mydomain.com to the google apps email myname@mydomain.com 使用PHP设置“ mydomain.com/username”的任何简单,整洁的方法吗? - Any easy, noncluttering way to set up “mydomain.com/username” using PHP? .href.replace给我双重域名(http:// localhosthttp://mydomain.com) - .href.replace giving me double domain (http://localhosthttp://mydomain.com) 如何使用zend框架获取像mydomain.com/username这样的动态URL - how to get dynamic URL like mydomain.com/username using zend framework
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM