【问题标题】:Codeigniter Redirect or Load view with templatesCodeigniter 重定向或加载带有模板的视图
【发布时间】:2018-04-01 05:14:24
【问题描述】:

我不知道如何在谷歌上表达这个问题,所以我找不到任何答案。

在我的 Views 文件夹中,我有带有页眉、导航栏、页脚的模板文件夹。

每当我从控制器加载视图时,我都必须这样做,

$this->load->view('template/header');
$this->load->view('template/navbar');
$this->load->view('pages/pagename');
$this->load->view('template/footer');

我如何使用重定向来做到这一点?我不知道为什么,但是每当我看到成功登录或失败的代码 sn-ps 时,他们总是使用重定向功能而不是像上面那样加载视图。

例如:

function __construct() {
    parent::__construct();
    if($this->ion_auth->logged_in()==FALSE)
    {
        redirect('pages/login');
    }
}

或者我可以使用这个吗?这仍然可以接受吗?

function __construct() {
    parent::__construct();
    if($this->ion_auth->logged_in()==FALSE)
    {
      $this->load->view('template/header');
      $this->load->view('template/navbar');
      $this->load->view('pages/login');
      $this->load->view('template/footer');
    }
}

【问题讨论】:

    标签: codeigniter url-redirection


    【解决方案1】:

    您可以使用重映射而不是构造。如果用户已登录,则重定向

    重新映射

    public function _remap($method, $params = array()){
       if(method_exists($this, $method)){
          if($this->ion_auth->logged_in()==FALSE){
             return call_user_func_array(array($this, $method), $params); //home page
          }
          return call_user_func_array(array($this, 'login'), $params); //if not logged in
      }
      show_404();
    }
    

    登录

    public function login() {
      $this->load->view('template/header');
      $this->load->view('template/navbar');
      $this->load->view('pages/login');
      $this->load->view('template/footer');
    }
    

    【讨论】:

      【解决方案2】:
      function __construct() {
          parent::__construct();
          if($this->ion_auth->logged_in()==FALSE)
          {
              redirect('controller/login');
          }
      }
      

      在您的控制器中,创建一个名为 login 的函数

      function login() {
        $this->load->view('template/header');
        $this->load->view('template/navbar');
        $this->load->view('pages/login');
        $this->load->view('template/footer');
      }
      

      【讨论】:

        【解决方案3】:

        在重定向中你需要使用 controller/method_name

        redirect('controllername'); 
        

        redirect('controllername/method');
        

        【讨论】:

        猜你喜欢
        • 2014-11-29
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-04-25
        • 2014-02-18
        • 1970-01-01
        • 1970-01-01
        • 2015-04-04
        相关资源
        最近更新 更多