在读取控制器中的所有方法之前如何检查条件。检查条件后,如果条件为TRUE,则仅必须访问控制器中的方法。如果条件返回false,则应将其重定向到另一个控制器。我该如何实现?
谢谢。
答案 0 :(得分:0)
控制器每次初始化时都会自动运行结构,然后再运行其他方法,因此您可以使用它来设置访问其他控制器方法所需的条件。
示例:
class ExampleController extends CI_Controller {
public function __construct () {
// use construct method of CI_Controller(the parent)
// don't foget this because it wont work without it!
parent::__construct();
if(true) {
//do the thing you want
} else {
// use the url helper to redirect to another page/controller
$this->load->helper('url');
redirect('another-page');
}
}
public function index() {
//display index page...
}
}