【问题标题】:ErrorException: Warning: Header may not contain more than a single header, new line detectedErrorException:警告:标头可能不包含多个标头,检测到新行
【发布时间】:2013-07-17 12:03:46
【问题描述】:

在某个发送电子邮件的功能之后,我无法重定向! 我的功能是:

public function emailAction($emails, $url)
{

    foreach($emails as $email)
    {
        $message = \Swift_Message::newInstance()
            ->setSubject('Updates in Symfony Blog')
            ->setFrom(array('blog@symfonyblog.com' => 'Symfony Blog'))
            ->setTo($email["email"])
            ->setBody(
            $this->renderView(
                'NEWSBlogBundle:Default:email.txt.twig',
                array('url' => $url)
            )
        )
        ;
        $this->get('mailer')->send($message);
    }

    return $this->redirect($this->newpostAction());
    //return $this->redirect($this->generateUrl('NEWSBlogBundle_homepage'));

}

它可以发送电子邮件,但是在重定向时,会出现错误:

ErrorException: 警告:标头可能不包含多个标头,在 C:\path\to\webroot\Symfony\app\cache\dev\classes.php 第 3899 行中检测到新行

  1. 在 C:\path\to\webroot\Symfony\app\cache\dev\classes.php 第 3899 行
  2. 在 ErrorHandler->handle('2', '标头不能包含多个标头,检测到新行', 'C:\path\to\webroot\Symfony\app\cache\dev\classes.php ', '3899', array('this' => object(RedirectResponse), 'name' => 'Location', 'values' => array('/app_dev.php/blog', object(ResponseHeaderBag), ' 重定向to /app_dev.php/blog 重定向到/app_dev.php/blog.', '1.0', '302', 'Found', null), 'value' => object(ResponseHeaderBag)))
  3. 在标头('位置:缓存控制:无缓存日期:星期三,2013 年 7 月 17 日 11:56:17 GMT 位置:/app_dev.php/blog ',假)在 C:\path\to\webroot \Symfony\app\cache\dev\classes.php 第 3899 行
  4. 在 Response->sendHeaders() 在 C:\path\to\webroot\Symfony\app\cache\dev\classes.php 第 3914 行
  5. 在 C:\path\to\webroot\Symfony\web\app_dev.php 第 27 行中的 Response->send() 处

虽然我没有更改任何有关“标题”的任何内容(而且我真的不知道它是关于什么的!)。我的 app_dev.php 是:

<?php

use Symfony\Component\HttpFoundation\Request;

if (isset($_SERVER['HTTP_CLIENT_IP'])
    || isset($_SERVER['HTTP_X_FORWARDED_FOR'])
    || !in_array(@$_SERVER['REMOTE_ADDR'], array('127.0.0.1', 'fe80::1', '::1'))
) {
    header('HTTP/1.0 403 Forbidden');
    exit('You are not allowed to access this file. Check '.basename(__FILE__).' for more information.');
}

$loader = require_once __DIR__.'/../app/bootstrap.php.cache';
require_once __DIR__.'/../app/AppKernel.php';

$kernel = new AppKernel('dev', true);
$kernel->loadClassCache();
Request::enableHttpMethodParameterOverride();
$request = Request::createFromGlobals();
$response = $kernel->handle($request);
$response->send();
$kernel->terminate($request, $response);

我尝试了两种不同的重定向方式(通过路由和直接调用控制器),它们给出了相同的错误,而如果我输入他们的 url,页面就会加载!

我不知道这个错误是什么以及我应该如何解决它!

【问题讨论】:

  • 也许您的文件中某处有空格输出
  • $this-&gt;newpostAction()的返回值是多少
  • @Pazi 这是一个加载页面的控制器函数。
  • 我没有弄清楚问题出在哪里,但我删除了“函数”并将其功能添加到调用它的控制器中,我的问题就解决了!

标签: php redirect symfony


【解决方案1】:

具体来说,您的问题是$this-&gt;newpostAction() 的返回值。通常它是一个Response 对象。但是重定向需要一个 uri/route,它可以通过 Location 标头发送到客户端。重定向始终是客户端转身。但是您可以使用-&gt;forward('AcmeBundle:Demo:index') 在内部转发到另一个控制器操作。在这种情况下,客户端上的 URL 不会更改。

【讨论】:

  • 感谢您的解释!
【解决方案2】:

你触发了 Symfony 的健全性检查。使用header() 发送标头时,理论上您可以通过一个函数调用发送大量标头。 Symfony 可以防止您在传递带有换行符作为目标 URL 的内容时意外这样做。

【讨论】:

  • 标头是如何发送的?如果我尝试返回 $this->redirect($this->generateUrl('NEWSBlogBu​​ndle_homepage'));从不同的控制器它工作正常!
  • 我没有弄清楚问题出在哪里,但我删除了“函数”并将其功能添加到调用它的控制器中,我的问题就解决了!
猜你喜欢
  • 1970-01-01
  • 2021-07-31
  • 2017-06-20
  • 1970-01-01
  • 2013-12-27
  • 2013-04-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多