【问题标题】:Redirecting to a certain controller function if validation failed in codeigniter如果在 codeigniter 中验证失败,则重定向到某个控制器函数
【发布时间】:2017-01-25 05:56:55
【问题描述】:

如果未授权访问同一控制器中的其他功能,我将尝试重定向到控制器索引。根据我的编码,它看起来像无限循环。请帮我这样做。

class Customer_Dashboard extends CI_Controller {
    public function __construct() {
        $method= $this->router->fetch_method();
        if ($this->session->userdata("cus_sel_comp")) {

        }else{
            if($method !="index"){
                redirect(base_url()."customer_dashboard");exit;
            }
        }
    }
    public function index() {
        // Here do some operations and let the user to select company and update the "cus_sel_comp" session variable. After set that session user can access the other controller functions.
    }
    public function other_function1() {

    }
    public function other_function2() {

    }
}

我的编码如上。我需要使用相同的控制器来执行此操作。问题是如果该会话未设置,则存在无限循环。

【问题讨论】:

    标签: php codeigniter redirect construct


    【解决方案1】:

    而不是重定向返回索引函数。请看下面的代码

     if($method !="index"){
                  return $this->index();
     }
    

    【讨论】:

      【解决方案2】:

      您正在调用相同的函数并将其重定向到相同的方法。

          class Customer_Dashboard extends CI_Controller {
              public function __construct() {
                  $method= $this->router->fetch_method();
                  if ($this->session->userdata("cus_sel_comp")) {
      
                  }else{
                      if($method !="index"){
                          redirect(base_url()."Customer_Dashboard/index"); // Redirect it to index if other method is invoked.
                      }
                  }
              }
              public function index() {
                  // Here do some operations and let the user to select company and update the "cus_sel_comp" session variable. After set that session user can access the other controller functions.
              }
              public function other_function1() {
      
              }
              public function other_function2() {
      
              }
          }
      

      也不要使用base_url() 而不是在配置中定义路径 base_url() 有许多其他条目,它们是不必要的。

      【讨论】:

      • 你能给我一个解决方案吗?
      • @GayanFernando 我回答了你关于重定向的问题吗?您是要解决这个问题还是 base_url 一个?
      • 我需要重定向到 customer_dashboard 控制器索引函数。不适用于站点主控制器。那有可能吗?或者除了使用重定向之外还有其他方法吗?
      • 检查答案。我已将其重定向到 customer_dashboard 索引函数的索引
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-10-06
      • 2012-08-18
      • 2011-07-24
      • 1970-01-01
      • 1970-01-01
      • 2021-08-24
      • 2018-06-28
      相关资源
      最近更新 更多