开发者

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

I a开发者_如何学运维m re-writing an old project using Symfony2 so I can try out the framework. I have urls that will be of the form:

/profile/{id}

in the snazzy way that Symfony2 does it. 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. I think the best option is to match "/profile.php" then in the controller, check that "id" exists in query-string and redirect accordingly. If it does not, I will redirect to 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).

  2. You write a mapper within Symfony. 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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜