Symfony: email address as request parameter

喜欢而已 提交于 2019-12-01 03:41:33

By default, Symfony treats . and / as parameter separators.
That makes it very easy to match a url like so:

/some/path/:param.:ext

But doesn't help with email addresses.

Fortunately, you can override the . separator by specifying your own pattern.
Just add the requirements line below to your routing:

unsubscribeform:
  url:  /unsubscribe/email/:email
  param: { module: subscribe, action: index }
  requirements: { email: .+ }

The .+ in the requirement is a regular expression matching anything. The .matches any character, and the + means match one-or-more.

(Tested in Symfony 1.4)

I don't know what you are doing in Symfony but it may help to be clear that the following is not a valid URL:

example.com/unsubscribe/email/me@example.com

What you almost certainly want (and this is true for all browsers!) is:

http://example.com/unsubscribe/email/me%40example.com

Note: The @ symbol is not safe and must be encoded, the . symbol is safe however (RFC1738). If you don't escape the @ symbol it will almost certainly cause big trouble so (escaping the . almost certainly won't, but you don't need to so I wouldn't).

Problems will occur with not escaping it because @ is reserved as a separator when passing authentication parameters (e.g. http://username:password@hostname.domain/url/). Some URL parsers will work out that you really meant to type %40 if @ occurs after the domain in the URL, but others won't.

Rather than just encoding the @ symbol statically you should use one of PHP's URL encoding functions on the email address( e.g. "$emailAddress = urlencode($emailAddress);") to ensure other characters in the address are also escaped properly. Don't be tempted to leave this till later or 'after you get it working' do it from the start and save yourself and the end users a headache! :-)

NB: There are several ways to encode URL's in PHP, so you will need to read through the documentation page for urlencode() and compare it with other approaches like rawurlencode() to be sure it's what you really want in your case.

Your issue isn't with Rewrite Rules. Considering that Symfony is throwing the exception then the request is making it to Symfony.

Can you post the traceback for the exception? If you have sf_logging_enabled it should log some pretty useful info for debugging the routing.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!