when I use function header() in controller - redirect not work, is it normal in Zend?
Ps... But if I add exit() after header() - this work. And If I use $this->_redirect - is work.
when I use function header() in controller - redirect not work, is it normal in Zend?
Ps... But if I add exit() after header() - this work. And If I use $this->_redirect - is work.
Yeah, this is pretty normal.
PHP frameworks like ZF1 typically create/expose a Response
object that represents the response that your server-side application wants to send to the waiting (typically browser) client. It typically encapsulates the HTTP headers and body that you wish to send. ZF1 is probably overriding your manual header()
call and replacing it with headers from the Response
object. That's why your header()
call appears effective when followed immediately by an exit()
; you're not giving the framework a chance to use its own headers from the Response
object.
As you appear to be aware, ZF1 controllers expose a Redirector
action helper that can set the desired headers on this Response object and immediately send the response. For example:
// Get a reference to the redirector
$redirector = $this->_helper->getHelper('Redirector');
// To redirect to a given url
$redirector->gotoUrl($url);
// To redirect to a given module, controller, and action
// $controller is optional, defaults to current controller
// $module is optional, defaults to current module
$redirector->gotoSimple($action, $controller, $module);
// To redirect to a named route with specified parameters
$redirector->gotoRoute($route, $params);
To set HTTP response codes, as well, you can do something like:
$redirector->setCode(301)->setGoToUrl('/some/url')->redirectAndExit();
See docs for the Redirector action helper.