如何读取控制器的所有方法之前检查的条件。条件检查后,如果这是真的,那么只有在控制器中的方法必须是可访问的。如果条件返回false,那么它应该被重定向到另一个控制器。我怎样才能做到这一点?
提前致谢。
控制器自动运行构建每次进行初始化时,它运行任何其他方法之前,所以你可以使用它来设置访问其他控制器方法所需的条件。
例:
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...
}
}