简体   繁体   English

如何使用Symfony2中的查询字符串路由URL?

[英]How do I route a URL using a query string in Symfony2?

I am re-writing an old project using Symfony2 so I can try out the framework. 我正在使用Symfony2重写一个旧项目,所以我可以试用这个框架。 I have urls that will be of the form: 我有以下形式的网址:

/profile/{id}

in the snazzy way that Symfony2 does it. 以Symfony2做的时髦方式。 However, the same page was originally found by doing: 但是,最初通过执行以下操作找到了同一页面:

/profile.php?id=12345

So, in case someone has an old URL, I'd like to redirect these links. 因此,如果某人有旧网址,我想重定向这些链接。 The problem is, I don't know how to catch routes of this nature. 问题是,我不知道如何捕捉这种性质的路线。 I tried 我试过了

/profile.php?id={id}

but that didn't seem to work. 但这似乎不起作用。 How can I set up this route? 我该如何设置这条路线?

Follow-up: I do not want to do "catch-all" (because it is non-intuitive to me so I fear future bugs), and I would prefer not to do it via htaccess for the same reason. 后续行动:我不想做“全能”(因为它对我来说不直观,所以我担心未来的错误),我宁愿不通过htaccess出于同样的原因这样做。 I think the best option is to match "/profile.php" then in the controller, check that "id" exists in query-string and redirect accordingly. 我认为最好的选择是匹配“/profile.php”然后在控制器中,检查查询字符串中是否存在“id”并相应地重定向。 If it does not, I will redirect to 404. 如果没有,我将重定向到404。

I see two options here: 我在这里看到两个选项:

  1. You map your old schema (/profile.php?id=54321) onto the new (/profile/54321) using mod_rewrite (in case you use Apache). 使用mod_rewrite将旧模式(/profile.php?id=54321)映射到新模式(/ profile / 54321)(如果使用Apache)。

  2. You write a mapper within Symfony. 你在Symfony中写了一个mapper。 That means at the end of your list of routes you specify a pattern that will just catch everything not yet catched: 这意味着在您的路由列表的末尾,您指定了一个模式,该模式将捕获尚未捕获的所有内容:


whatever:
  pattern: /{whatever}
  defaults: { _controller: CoreBundle:Default:whatever }
  requirements:
    whatever: .+

For (2) you will have to check what the Request-object offers you for the queries (like getQueryString()), b/c I am not sure if it is possible to have something like ?xyz being matched in a route. 对于(2),您将必须检查Request-object为查询提供的内容(如getQueryString()),b / c我不确定是否可以在路由中匹配?xyz。

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

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