ErrorException: Warning: header can contain at most one header, newline encountered
I'm having trouble redirecting after a certain function that sends emails! My function:
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 emails ok, but then when redirected, it throws an error:
ErrorException: warning: header can contain at most one header, newline found at C: \ path \ to \ webroot \ Symfony \ app \ cache \ dev \ classes.php line 3899
- in folder C: \ path \ to \ webroot \ Symfony \ app \ cache \ dev \ classes.php 3899
- in ErrorHandler-> handle ('2', 'Header can contain at most one header, newline detected', 'C: \ path \ to \ webroot \ Symfony \ app \ cache \ dev \ classes.php', '3899' , array ('this' => object (RedirectResponse), 'name' => 'Location', 'values' => array ('https://stackoverflow.com/app_dev.php/blog', object (ResponseHeaderBag ), 'Redirect tohttps: //stackoverflow.com/app_dev.php/blog Redirect to https://stackoverflow.com/app_dev.php/blog .', '1.0', '302', 'Found', null), 'value' => object (ResponseHeaderBag)))
- in the header ('Location: Cache-Control: no-cache Date: Wed, 17 Jul 2013 11:56:17 GMT Location: https: //stackoverflow.com/app_dev.php/blog, false) in C: \ path \ to \ webroot \ Symfony \ app \ cache \ dev \ classes.php line 3899
- in Response-> sendHeaders () in folder C: \ path \ to \ webroot \ Symfony \ app \ cache \ dev \ classes.php line 3914
- in Response-> send () at line C: \ path \ to \ webroot \ Symfony \ web \ app_dev.php 27
although I haven't changed anything about any "headers" (and don't know what they are!). My 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);
I've tried two different ways to redirect both (via routing and direct controller call), of which the same error while the pages are loading if I jusr type in their url!
I have no idea what the error is and how I should fix it!
source to share
To be detailed, your problem was a return value $this->newpostAction()
. This is usually an object Response
. But the redirect expects a uri / route that can be sent to the client via a header Location
. Forwarding always happens on the client side. But you can use ->forward('AcmeBundle:Demo:index')
to forward within another controller action. In this case, the client URL is not changed.
source to share