我正在尝试创建一个基于角色的系统,所以我在网上找到了这个教程:
https://www.youtube.com/watch?v=eOzmO61CsVg&t=619s
所以我开始通过观看视频来创建控制器。
在我创建了MY_Controller类后,我将Page Class扩展为MY_Controller类并覆盖$access
变量以检查它是否正常工作,我试图在MY_Controller类中打印$access
变量
但是当我死的时候总是给*,可能是我做错了。
下面是代码:
MY_Controller类:
class MY_Controller extends CI_Controller {
/**
* '*' all user
* '@' logged in user
* 'Hospital' for hospital users
* 'Reciever' for recievers
* @var string
*/
private $access = "*";
public function __construct() {
parent::__construct();
$this->login_check();
}
public function login_check(){
if ($this->access != "*")
{
// here we check the role of the user
if (! $this->permission_check()) {
die("<h4>Access denied</h4>");
}
// if user try to access logged in page
// check does he/she has logged in
// if not, redirect to login page
if (! $this->session->userdata("logged_in")) {
redirect("auth");
}
}
die($this->access);
}
public function permission_check(){
if ($this->access == "@") {
return true;
}
else
{
$access = is_array($this->access) ?
$this->access :
explode(",", $this->access);
if (in_array($this->session->userdata("role"), array_map("trim", $access)) ) {
return true;
}
return false;
}
}
}
页面分类:
class Pages extends MY_Controller {
protected $access="Admin";
public function view($page='available_samples') {
if(!file_exists(APPPATH.'views/pages/'.$page.'.php')){
show_404();
}
$data['title'] = $page;
$this->load->view('templates/header',$data);
$this->load->view('pages/'.$page);
$this->load->view('templates/footer');
}
}
我得到了什么
我的$access
变量值为* always。
预期行为
Pages类中$access
变量的值应覆盖MY_Controller类中的访问变量值。