【问题标题】:PHP MVC correct redirecting, is die needed?PHP MVC 正确重定向,需要死吗?
【发布时间】:2015-10-06 13:32:26
【问题描述】:

在 PHP MVC 结构中重定向到另一个控制器的正确方法是什么?

假设我有我的控制器类:

abstract class controller {

    public function __construct() {
        // code here
    }

    abstract protected function index();

    protected function redirect($location = 'index') {
        header('location:' . URL . $location);
        die(); // <-not shure if this die is right here
    }

}

在另一个类中,我需要验证一个条件,如果为假则重定向到另一个地方

class loginController extends controller {

    public function __construct() {
        parent::__construct();
        // More code here
    }

    public function index() {
        if ($this->session->verifyUserStatus()) {
            $this->redirect('admin');
        }
        $this->view->render('login');
    }
}

我是否需要在重定向器末尾调用 die() 函数?还是正常结束脚本就可以了?

问候

【问题讨论】:

    标签: php redirect model-view-controller exit


    【解决方案1】:

    你可以简单地正常结束它,die() 不是必需的。

    【讨论】:

      【解决方案2】:

      在理想情况下,您不会明确地杀死它,因为框架可能还有其他未注册为关闭信号的事情要做。这样的框架也不需要在控制器内使用header() 函数,因为这是不可测试的。相反,控制器可以执行return Redirect::to($path); 之类的操作,并让框架设置适当的标头。

      如果处理质量差的程序代码,明确地杀死它是可以的。

      【讨论】:

      • 但我的猜测是不杀死它会导致脚本继续并呈现登录视图,我知道它会发生得非常快并且用户不会注意到它,但是如果我有时间密集型功能?
      • Browsers are configured to ignore the body 如果存在Location 标头。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-01-21
      • 2011-09-15
      • 1970-01-01
      • 2017-10-08
      • 2016-03-29
      • 2017-11-16
      • 1970-01-01
      相关资源
      最近更新 更多