我正在使用CodeIgniter。我有一个名为abc的控制器,还有名为index、a、b和c的函数。
www.example.com/abc/我要那个用户只能访问他登录的区域。
www.example.com/abc/ //if loggged in else back to homepage或
www.example.com/abc/a/ //if loggged in else back to homepage现在检查登录。我用:
if($this->auth->is_logged_in()) { .. } else { redirect('/'); }
每个功能都是独立的。
还有其他方法吗??
发布于 2013-09-13 21:52:03
我认为您可以通过重写构造函数并在其中调用您的函数来做到这一点。
<?php
class Blog extends CI_Controller {
public function __construct()
{
parent::__construct();
// check login
}
}
?>发布于 2013-09-13 22:00:40
对于特定的控制器,您可以将if签入控制器的constructor中,以便当调用控制器的任何方法时,它将通过您的if检查。
class Abc extends CI_Controller {
public function __construct()
{
parent::__construct();
//your if goes here
}
}如果要检查用户是否已登录到整个应用程序中,则可以使用构造函数方法__construct() of CI_Controller,以便在用户访问应用程序中的任何控制器时对其进行验证。
class CI_Controller {
private static $instance;
/**
* Constructor
*/
public function __construct()
{
//your if goes here
}
}https://stackoverflow.com/questions/18795771
复制相似问题