从模块 class onbootstrap 方法执行重定向
perform a redirect from module class onbootstrap method
是否有使用 zend 框架在 Application Module class
内部执行重定向的正确方法?
我想重构此代码以使用基于 zend 的重定向。
class Moudle
{
public function onBootstrap(MvcEvent $e)
{
$arg = 1;
if($arg==1){
header("Location: index.php?redirect=1");
exit();
}
}
我不知道其他人是怎么做的,但这里有一个我已经在项目中做过的例子:
class Module
{
public function onBootstrap(MvcEvent $e)
{
$arg = 1;
if($arg==1){
$response = $e->getResponse();
$response->getHeaders()->clearHeaders()->addHeaderLine('Location', '/index.php?redirect=1');
$response->setStatusCode(302)->sendHeaders();
exit();
}
};
如果您不想对 url 进行硬编码(我不推荐这样做),那么您可以使用视图助手 url 或控制器插件 url 你从服务经理那里得到的,以便使用你的路由名称而不是“/index.php?redirect=1”。我总是喜欢在我的 ZF 项目中避免硬编码 url,因为我们可以使用路由的名称。由你决定。
是否有使用 zend 框架在 Application Module class
内部执行重定向的正确方法?
我想重构此代码以使用基于 zend 的重定向。
class Moudle
{
public function onBootstrap(MvcEvent $e)
{
$arg = 1;
if($arg==1){
header("Location: index.php?redirect=1");
exit();
}
}
我不知道其他人是怎么做的,但这里有一个我已经在项目中做过的例子:
class Module
{
public function onBootstrap(MvcEvent $e)
{
$arg = 1;
if($arg==1){
$response = $e->getResponse();
$response->getHeaders()->clearHeaders()->addHeaderLine('Location', '/index.php?redirect=1');
$response->setStatusCode(302)->sendHeaders();
exit();
}
};
如果您不想对 url 进行硬编码(我不推荐这样做),那么您可以使用视图助手 url 或控制器插件 url 你从服务经理那里得到的,以便使用你的路由名称而不是“/index.php?redirect=1”。我总是喜欢在我的 ZF 项目中避免硬编码 url,因为我们可以使用路由的名称。由你决定。