代码之家  ›  专栏  ›  技术社区  ›  Muhammad Raheel

Codeigniter重定向最佳选择方法

  •  0
  • Muhammad Raheel  · 技术社区  · 13 年前

    在同一个控制器中,我发现有两种重定向方式。

    Class Home extends mY_Controller{
    
        public $view;
        public $redirect;
        function first_method
        {
            $this->view =   FALSE;
            //   $this->second_method();
            //   redirect('Home/second_method');            
        }
    
        function second_method()
        {
            echo 'Second one';
        }       
    } 
    

    我也想知道哪种方法最好 我在mycontroller中有一个重定向方法 如果某些条件不匹配,则使用重定向变量重定向用户 为了简单起见,我发布了这个

    <?php
    class MY_Controller extends CI_Controller 
    {
        protected $data;
    
        function __construct()
        {
            parent::__construct();
        }
    
        public function _remap($method, $parameters)
        {
            if($this->view === FALSE)
            {
                redirect($this->redirect);
            }else{
                $this->load->view('my_view');
            }   
    
        }
    }   
    

    使用第一种方法,我无法访问第二行

    1 回复  |  直到 13 年前
        1
  •  0
  •   Sergey Telshevsky    13 年前

    我建议使用 redirect() 因为以后更改HTTP响应代码很容易,如果您需要的话,也可以使其中一些代码 refresh 类型

    如果您发现以后有必要更改所有重定向,并且您甚至可以为它们添加一些逻辑,那么您可以始终覆盖帮助程序。

    推荐文章