可能重复: CodeIgniter身份验证+用户权限
我有5个用户类型和权限表,在其中我给予不同的用户不同的权限。如is_view,is_delete,is_add等。用户根据这些权限访问该功能。
我完成了数据库。在调用控制器之前,我想检查每个页面上给用户的权限。
发布于 2012-10-11 06:47:07
您应该将您的auth逻辑放在控制器的构造函数中。
或
在基本控制器的构造函数中(更枯燥,因为您不必在所有控制器中重复逻辑)。
发布于 2012-10-11 07:00:34
我会创建一个新的控制器来扩展核心控制器。将此文件放入application/core/中
class MY_AuthController extends CI_Controller {
public function __construct() {
// Do your auth check in here, redirect if not logged in
}
}然后,所有需要身份验证的页面,只需继承这个新控制器即可。这个文件你只是放在你的常规控制器-文件夹
class Admin extends MY_AuthController {
// All your controller goodness in here..
}发布于 2012-10-12 03:56:07
我建议你阅读以下两篇文章:
1.菲尔·斯特金在保持干燥上的帖子。
菲尔将向您介绍如何创建父控制器,其构造函数将包含会话和潜在的数据库逻辑。此后创建的所有控制器都应该从自定义控制器继承,而不是本机CI_Controller。
然后是..。
2. Shane‘s CodeIgniter基类更新。
Shane的文章修改了菲尔的技术,并将您的自定义控制器从/core重新定位到/base,并且还使用了一个更好的__autoload()'er。例如,这个实现允许我使用CodeIgniter的CLI类,而菲尔的CLI类被窃听了。
为了给您一个想法-您的代码看起来有点像这样,一旦完成:
在/base/MY_In_Controller.php中
<?php
class MY_In_Controller extends CI_Controller{
function __construct(){
parent::__construct();
//things like:
//is the user even logged in? thank heavens I don't have to check this in every controller now. redirect if the session doesnt exist.
//query the database and grab the permissions for the user. persist them with $this->load->vars();
$this->data['perms'] = some_database_function();
$this->load->vars($this->data);
}
}在controllers/manage.php中
<?php
class Manage extends MY_In_Controller{
function __construct(){
parent::__construct();
}
function index(){
$this->load->view('manage');
//and I can still access their permissions here and in the view.
print_r($this->data['perms']);
}
}https://stackoverflow.com/questions/12833504
复制相似问题