CodeIgniter通过hook的方式实现简单的权限控制

根据自己的实际情况,需要两个文件,一个是权限控制类,Acl,另外一个是权限配置的文件acl.php放在了config这个目录下。

Acl这个类放在了application/hook/acl.php。通过application/config/config.php文件开启hook,并且配置config这个目录下的hook.php文件。

1、开启hook功能,config.php这个文件

[php]  view plain copy
  1. /* 
  2. |-------------------------------------------------------------------------- 
  3. | Enable/Disable System Hooks 
  4. |-------------------------------------------------------------------------- 
  5. | 
  6. | If you would like to use the 'hooks' feature you must enable it by 
  7. | setting this variable to TRUE (boolean).  See the user guide for details. 
  8. | 
  9. */  
  10. $config['enable_hooks'] = TRUE;  

2、配置hook.php这个文件

[php]  view plain copy
  1. /* 
  2. | ------------------------------------------------------------------------- 
  3. | Hooks 
  4. | ------------------------------------------------------------------------- 
  5. | This file lets you define "hooks" to extend CI without hacking the core 
  6. | files.  Please see the user guide for info: 
  7. | 
  8. |   http://codeigniter.com/user_guide/general/hooks.html 
  9. | 
  10. */  
  11.   
  12. $hook['post_controller_constructor'] = array(  
  13.     'class'    => 'Acl',  
  14.     'function' => 'auth',  
  15.     'filename' => 'acl.php',  
  16.     'filepath' => 'hooks'  
  17. );  

具体的参数说明可以参看文档的链接地址,这里尤其要注意post_controller_constructor这个值,可以根据情况选择不同的。

3、编写权限配置文件acl.php放在config目录下。

[php]  view plain copy
  1. $config['AUTH'] = array(  
  2.     SUPER_ADMIN         => array(  
  3.         'admin' => array('index''logout'),  
  4.     ),  
  5.     ADMIN   => array(  
  6.         'admin' => array('index''logout'),  
  7.     ),  
  8.     GUEST => array(  
  9.         'admin' => array('index''logout'),  
  10.     ),  
  11. );  

这里只是我根据自己的情况定义的,不是真实数据,根据自己的情况定。还有主要变量名字要交$config,这样便于加载使用。

4、编写具体的权限控制Acl类

[php]  view plain copy
  1. class Acl {  
  2.   
  3.     private $url_model;  
  4.     private $url_method;  
  5.     private $CI;  
  6.    
  7.     function Acl()  
  8.     {  
  9.         $this->CI =& get_instance();  
  10.         $this->CI->load->library('session');  
  11.   
  12.         $this->url_model = $this->CI->uri->segment(1);  
  13.         $this->url_method = $this->CI->uri->segment(2);  
  14.     }  
  15.    
  16.     function auth()  
  17.     {  
  18.         $user = $this->CI->session->userdata('USER');  
  19.         if(empty($user))  
  20.             $user->status = 0;  
  21.   
  22.         $this->CI->load->config('acl');  
  23.         $AUTH = $this->CI->config->item('AUTH');  
  24.   
  25.         if(in_array($user->status, array_keys($AUTH))){  
  26.             $controllers = $AUTH[$user->status];  
  27.   
  28.             if(in_array($this->url_model, array_keys($controllers))){  
  29.                   
  30.                 if(!in_array($this->url_method, $controllers[$this->url_model])){  
  31.                     show_error('您无权访问该功能,该错误已经被记录!点击<a href="'. site_url('admin/logout') .'">返回</a>');  
  32.                 }  
  33.             }else{  
  34.                 show_error('您无权访问该模块,该错误已经被记录!点击<a href="'. site_url('admin/logout') .'">返回</a>');  
  35.             }  
  36.         }  
  37.         else  
  38.             show_error('错误的用户类型,该错误已经被记录!点击<a href="'. site_url('admin/logout') .'">返回</a>');  
  39.     }  
  40. }  

整体上大体是这样的形式,最后还是要根据自己的实际情况来确定。

需要注意的是:

[php]  view plain copy
  1. $this->CI =& get_instance();  
[php]  view plain copy
  1. //和第2步中的有关系。有的人配置后说初始化没值,这里请仔细阅读文档。<pre name="code" class="php" style="margin-top: 4px; margin-right: 0px; margin-bottom: 4px; margin-left: 0px; background-color: rgb(240, 240, 240); ">$hook['post_controller_constructor']</pre>  

此处并非真实数据,所以请勿照超,请领会精神。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
CodeIgniter本身并没有提供读写分离的功能,但是我们可以通过扩展CodeIgniter的数据库类来实现读写分离。 以下是一种简单实现方式: 1. 在application/core目录下创建MY_DB.php文件,内容如下: ``` class MY_DB extends CI_DB { protected $_slave_db = NULL; public function __construct($params = '') { parent::__construct($params); // 连接从数据库 $this->_slave_db = $this->CI->load->database('slave', TRUE); } /** * 执行读操作 */ public function read($sql, $binds = FALSE, $return_object = TRUE) { return $this->_slave_db->query($sql, $binds, $return_object); } /** * 执行写操作 */ public function write($sql, $binds = FALSE, $return_object = TRUE) { return $this->query($sql, $binds, $return_object); } } ``` 2. 在配置文件中设置主从数据库连接信息,application/config/database.php: ``` // 主数据库 $db['default'] = array( 'dsn' => '', 'hostname' => 'localhost', 'username' => 'root', 'password' => '', 'database' => 'db_master', 'dbdriver' => 'mysqli', 'dbprefix' => '', 'pconnect' => FALSE, 'db_debug' => (ENVIRONMENT !== 'production'), 'cache_on' => FALSE, 'cachedir' => '', 'char_set' => 'utf8', 'dbcollat' => 'utf8_general_ci', 'swap_pre' => '', 'encrypt' => FALSE, 'compress' => FALSE, 'stricton' => FALSE, 'failover' => array(), 'save_queries' => TRUE ); // 从数据库 $db['slave'] = array( 'dsn' => '', 'hostname' => 'localhost', 'username' => 'root', 'password' => '', 'database' => 'db_slave', 'dbdriver' => 'mysqli', 'dbprefix' => '', 'pconnect' => FALSE, 'db_debug' => (ENVIRONMENT !== 'production'), 'cache_on' => FALSE, 'cachedir' => '', 'char_set' => 'utf8', 'dbcollat' => 'utf8_general_ci', 'swap_pre' => '', 'encrypt' => FALSE, 'compress' => FALSE, 'stricton' => FALSE, 'failover' => array(), 'save_queries' => TRUE ); ``` 3. 在控制器中使用: ``` class Example extends CI_Controller { public function index() { $this->load->database(); // 执行读操作 $result = $this->db->read("SELECT * FROM `table`"); // 执行写操作 $this->db->write("INSERT INTO `table` (`name`, `age`) VALUES (?, ?)", array('John', 20)); } } ``` 这样,就可以实现简单的读写分离了。当然,实际的应用场景可能更加复杂,需要根据具体的情况进行调整和优化。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值