Перенаправление на определенную функцию контроллера, если проверка не прошла в codeigniter

Я пытаюсь перенаправить на индекс контроллера, если не авторизован доступ к другим функциям в том же контроллере. Согласно моей кодировке это выглядит как бесконечный цикл. Пожалуйста, помогите мне сделать это.

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() {

}
}

Моя кодировка такая же, как указано выше. Мне нужно сделать это, используя тот же контроллер. Проблема в том, что если этот сеанс не установлен, существует бесконечный цикл.

0

Решение

Вместо перенаправления функции возврата индекса. Смотрите код ниже

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

Другие решения

Вы вызываете ту же функцию и перенаправляете ее на тот же метод.

    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() Есть много других записей, которые не обязательно называются.

0

По вопросам рекламы ammmcru@yandex.ru
Adblock
detector