Symfony:电子邮件地址作为请求参数

2023-09-23php开发问题
6

本文介绍了Symfony:电子邮件地址作为请求参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

我在将 url 中的电子邮件地址传递给 symfony 应用程序时遇到了一些问题.

I'm having some issues with passing an email address in a url to a symfony app.

网址看起来像

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

它总是会导致 sfError404Exception,除非删除了句点.在进行了一些谷歌搜索之后,我看到的唯一解决方案是 htaccess 由于存在时间段而绕过了 url.但是,当我将建议的修复添加到 htaccess 时,如下所示:

It will always result in a sfError404Exception, except when the period is removed. After doing some googling around, the only solution I've yet seen is that htaccess is bypassing the url because of the period present. However, when I add the suggested fix to htaccess, like so:

# we skip all files with .something
RewriteCond %{REQUEST_URI} ..+$
RewriteCond %{REQUEST_URI} !@.+    #skip email address
RewriteCond %{REQUEST_URI} .epl$
RewriteCond %{REQUEST_URI} !.html$ 
RewriteCond %{REQUEST_URI} !.rhtml$
RewriteRule .* - [L]

我得到相同的 404.当我直接在 url (example.com/index.php/unsubscribe/email/me@example.com) 中使用前端控制器时,它也会返回 404.我试过将转义版本直接放入地址栏,例如 example.com/unsubscribe/me%40example%2Ecom 并且这有效,但仅在 Firefox 中,没有其他地方.

I get the same 404. It also returns 404 when I use the front controller directly in the url (example.com/index.php/unsubscribe/email/me@example.com). I've tried putting the escaped version directly into the address bar, eg example.com/unsubscribe/me%40example%2Ecom and this works, but only in firefox, nowhere else.

此时我已经在论坛上用了大约 2 个小时的谷歌搜索来回答问题,但我的想法已经用完了.

I've spent about 2 hours in forum answer googling hell at this point and I'm running out of ideas.

有什么想法吗?

谢谢.

更新:这是routing.yml的相关部分:

Update: Here is the relevant section of the routing.yml:

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

更新:堆栈跟踪......看起来它没有得到任何路由信息给我

Update: Stack trace ... looks like its not getting any route information to go on to me

404 | Not Found | sfError404Exception
Empty module and/or action after parsing the URL "/unsubscribe/email/me@example.com" (/).
stack trace

1. at ()
  in SF_SYMFONY_LIB_DIR/controller/sfFrontWebController.class.php line 44 ...
          41.
          42.       if (empty($moduleName) || empty($actionName))
          43.       {
          44.         throw new sfError404Exception(sprintf('Empty module and/or action after parsing the URL "%s" (%s/%s).', $request->getPathInfo(), $moduleName, $actionName));
          45.       }
          46.
          47.       // make the first request
2. at sfFrontWebController->dispatch()
  in SF_SYMFONY_LIB_DIR/util/sfContext.class.php line 159 ...
         156.    */
         157.   public function dispatch()
         158.   {
         159.     $this->getController()->dispatch();
         160.   }
         161.
         162.   /**
3. at sfContext->dispatch()
  in /home/web/htdocs/index.php line 10 ...
           7. require_once(SF_ROOT_DIR.DIRECTORY_SEPARATOR.'config'.DIRECTORY_SEPARATOR.'ProjectConfiguration.class.php');
           8.
           9. $configuration = ProjectConfiguration::getApplicationConfiguration(SF_APP, SF_ENVIRONMENT, SF_DEBUG);
          10. sfContext::createInstance($configuration)->dispatch();

11.

推荐答案

默认情况下,Symfony 将 ./ 视为参数分隔符.
这使得匹配这样的网址变得非常容易:

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.

幸运的是,您可以通过指定自己的模式来覆盖 . 分隔符.
只需将下面的 requirements 行添加到您的路由中:

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.

(在 Symfony 1.4 中测试)

(Tested in Symfony 1.4)

这篇关于Symfony:电子邮件地址作为请求参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

The End

相关推荐

PHP实现DeepL翻译API调用
DeepL的翻译效果还是很强大的,如果我们要用php实现DeepL翻译调用,该怎么办呢?以下是代码示例,希望能够帮到需要的朋友。 在这里需要注意,这个DeepL的账户和api申请比较难,不支持中国大陆申请,需要拥有香港或者海外信用卡才行,没账号的话,目前某宝可以...
2025-08-20 php开发问题
168

PHP通过phpspreadsheet导入Excel日期数据处理方法
PHP通过phpspreadsheet导入Excel日期,导入系统后,全部变为了4开头的几位数字,这是为什么呢?原因很简单,将Excel的时间设置问文本,我们就能看到该日期本来的数值,上图对应的数值为: 要怎么解决呢?进行数据转换就行,这里可以封装方法,或者用第三方的...
2024-10-23 php开发问题
287

mediatemple - 无法使用 codeigniter 发送电子邮件
mediatemple - can#39;t send email using codeigniter(mediatemple - 无法使用 codeigniter 发送电子邮件)...
2024-08-23 php开发问题
11

Laravel Gmail 配置错误
Laravel Gmail Configuration Error(Laravel Gmail 配置错误)...
2024-08-23 php开发问题
16

将 PHPMailer 用于 SMTP 的问题
Problem with using PHPMailer for SMTP(将 PHPMailer 用于 SMTP 的问题)...
2024-08-23 php开发问题
4

关于如何在 GoDaddy 服务器中使用 PHPMailer 设置 SMTP 的问题
Issue on how to setup SMTP using PHPMailer in GoDaddy server(关于如何在 GoDaddy 服务器中使用 PHPMailer 设置 SMTP 的问题)...
2024-08-23 php开发问题
17