Zend Framework v1 - 当我在控制器中使用 header() 时 - 重定向不起作用
Zend Framework v1 - when I use header() in controller - redirect not work
当我在控制器中使用函数 header() 时 - 重定向不起作用,在 Zend 中是否正常?
Ps...
但是如果我在 header() 之后添加 exit() - 这个工作。
如果我使用 $this->_redirect - 就可以了。
是的,这很正常。
PHP 像 ZF1 这样的框架通常 create/expose 一个 Response
object 表示您的 server-side 应用程序想要发送给等待的响应(通常浏览器)客户端。它通常封装您希望发送的 HTTP headers 和 body。 ZF1 可能会覆盖您的手动 header()
调用并将其替换为 Response
object 中的 headers。这就是为什么您的 header()
调用在紧接着 exit()
时显示有效的原因;您没有让框架有机会使用 Response
object.
中自己的 headers
如您所知,ZF1 控制器公开了一个 Redirector
操作助手,可以在此响应 object 上设置所需的 headers 并立即发送响应。例如:
// 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);
要设置 HTTP 响应代码,您也可以执行以下操作:
$redirector->setCode(301)->setGoToUrl('/some/url')->redirectAndExit();
请参阅 Redirector action helper 的文档。
当我在控制器中使用函数 header() 时 - 重定向不起作用,在 Zend 中是否正常?
Ps... 但是如果我在 header() 之后添加 exit() - 这个工作。 如果我使用 $this->_redirect - 就可以了。
是的,这很正常。
PHP 像 ZF1 这样的框架通常 create/expose 一个 Response
object 表示您的 server-side 应用程序想要发送给等待的响应(通常浏览器)客户端。它通常封装您希望发送的 HTTP headers 和 body。 ZF1 可能会覆盖您的手动 header()
调用并将其替换为 Response
object 中的 headers。这就是为什么您的 header()
调用在紧接着 exit()
时显示有效的原因;您没有让框架有机会使用 Response
object.
如您所知,ZF1 控制器公开了一个 Redirector
操作助手,可以在此响应 object 上设置所需的 headers 并立即发送响应。例如:
// 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);
要设置 HTTP 响应代码,您也可以执行以下操作:
$redirector->setCode(301)->setGoToUrl('/some/url')->redirectAndExit();
请参阅 Redirector action helper 的文档。