ErrorException: 警告: 标题不能包含多个标题,检测到新行 [英] ErrorException: Warning: Header may not contain more than a single header, new line detected

查看:121
本文介绍了ErrorException: 警告: 标题不能包含多个标题,检测到新行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am having trouble redirecting after a certain function that sends emails! My function is:

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'));

}

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

It sends the emails OK, but then when redirecting, gives the error:

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

ErrorException: Warning: Header may not contain more than a single header, new line detected in C:\path\to\webroot\Symfony\app\cache\dev\classes.php line 3899

  1. 在 C:\path\to\webroot\Symfony\app\cache\dev\classes.php 第 3899 行
  2. at ErrorHandler->handle('2', 'Header 可能不包含多个标题,检测到新行', '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), '重定向到/app_dev.php/blog 重定向到 /app_dev.php/blog.', '1.0', '302', 'Found', null),'value' => object(ResponseHeaderBag)))
  3. at header('Location: Cache-Control: no-cache Date: Wed, 17 Jul 2013 11:56:17 GMT Location:/app_dev.php/blog ', false) in C:\path\to\webroot\Symfony\app\cache\dev\classes.php 行 3899
  4. 在 C:\path\to\webroot\Symfony\app\cache\dev\classes.php 行 3914 中的 Response->sendHeaders()
  5. 在 C:\path\to\webroot\Symfony\web\app_dev.php 第 27 行中的 Response->send()
  1. in C:\path\to\webroot\Symfony\app\cache\dev\classes.php line 3899
  2. at ErrorHandler->handle('2', 'Header may not contain more than a single header, new line detected', '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), ' Redirecting to /app_dev.php/blog Redirecting to /app_dev.php/blog. ', '1.0', '302', 'Found', null), 'value' => object(ResponseHeaderBag)))
  3. at header('Location: Cache-Control: no-cache Date: Wed, 17 Jul 2013 11:56:17 GMT Location: /app_dev.php/blog ', false) in C:\path\to\webroot\Symfony\app\cache\dev\classes.php line 3899
  4. at Response->sendHeaders() in C:\path\to\webroot\Symfony\app\cache\dev\classes.php line 3914
  5. at Response->send() in C:\path\to\webroot\Symfony\web\app_dev.php line 27

虽然我没有改变任何关于标题"的东西(我真的不知道它是关于什么的!).我的 app_dev.php 是:

while I haven't changed anything about any "headers" (and don't really know what it is about!). My app_dev.php is:

<?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,页面就会加载!

I tried two different ways of redirecting both (through routing and directly calling the controller) of which give the same error, whilst the pages load if I jusr type in their url!

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

I have NO idea what this error is and how I should fix it!

推荐答案

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

To be detail, your problem was the return value of $this->newpostAction(). Usally it's a Response object. But a redirect expects an uri/route, which can be sent to the client via the Location header. A redirect is always a client side turn around. But you can use ->forward('AcmeBundle:Demo:index') to forward internally to another controller action. In this case the URL on the client is not changed.

这篇关于ErrorException: 警告: 标题不能包含多个标题,检测到新行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆