標籤:指令碼 管理 php application search
<span style="background-color: rgb(247, 252, 255); font-family: Verdana, Arial, Helvetica, sans-serif; "></span><p><span style="font-family: Verdana, Arial, Helvetica, sans-serif;
font-size: 14px;
background-color: rgb(247, 252, 255);
">CI 的鉤子功能使得您可以在不修改系統核心檔案的基礎上來改變或增加系統的核心運行功能。</span></p><p><span style="font-family: Verdana, Arial, Helvetica, sans-serif;
font-size: 14px; background-color: rgb(247, 252, 255); ">
例如,您可以在控制器剛剛載入前或剛剛載入後來運行特定的指令碼,或者在其他時刻來觸發您的指令碼。
</span></p><p><span style="font-family: Verdana, Arial, Helvetica, sans-serif;
font-size: 14px;
background-color: rgb(247, 252, 255);
">看代碼:</span></p><pre name="code" class="php"><span style="background-color: rgb(247, 252, 255); font-family: Verdana, Arial, Helvetica, sans-serif; ">
</span>
system/application/config/hooks.php中添加鉤子聲明:
[php]
$hook[‘post_controller_constructor‘] = array(
‘class‘ => ‘Acl‘,
‘function‘ => ‘filter‘,
‘filename‘ => ‘acl.php‘,
‘filepath‘ => ‘hooks‘,
);
system/application/config/config.php中讓鉤子系統生效
$config[‘enable_hooks‘] = TRUE;
然後在中建立acl.php許可權系統設定檔,當然你也可以放在資料庫中。
//遊客許可權映射
$config[‘acl‘][‘visitor‘] = array(
‘‘ => array(‘index‘),//首頁 www.2cto.com
‘music‘ => array(‘index‘, ‘list‘),
‘user‘ => array(‘index‘, ‘login‘, ‘register‘)
);
//管理員
$config[‘acl‘][‘admin‘] = array(
);
//-------------配置許可權不夠的提示資訊及跳轉url------------------//
$config[‘acl_info‘][‘visitor‘] = array(
‘info‘ => ‘需要登入以繼續‘,
‘return_url‘ => ‘user/login‘
);
$config[‘acl_info‘][‘more_role‘] = array(
‘info‘ => ‘需要更高許可權以繼續‘,
‘return_url‘ => ‘user/up‘
);
/* End of file acl.php */
/* Location: ./application/config/acl.php */
system/application/hooks目錄下添加acl.php邏輯處理檔案
class Acl
{
private $url_model;//所訪問的模組,如:music
private $url_method;//所訪問的方法,如:create
private $url_param;//url所帶參數 可能是 1 也可能是 id=1&name=test
private $CI;
function Acl()
{
$this->CI = & get_instance();
$this->CI->load->library(‘session‘);
$url = $_SERVER[‘PHP_SELF‘];
$arr = explode(‘/‘, $url);
$arr = array_slice($arr, array_search(‘index.php‘, $arr) + 1, count($arr));
$this->url_model = isset($arr[0]) ? $arr[0] : ‘‘;
$this->url_method = isset($arr[1]) ? $arr[1] : ‘index‘;
$this->url_param = isset($arr[2]) ? $arr[2] : ‘‘;
}
function filter()
{
$user = $this->CI->session->userdata(‘user‘);
if (emptyempty($user)) {//遊客visitor
$role_name = ‘visitor‘;
} else {
$role_name = $user->role;
}
$this->CI->load->config(‘acl‘);
$acl = $this->CI->config->item(‘acl‘);
$role = $acl[$role_name];
$acl_info = $this->CI->config->item(‘acl_info‘);
if (array_key_exists($this->url_model, $role) && in_array($this->url_method, $role[$this->url_model])) {
;
} else {//無許可權,給出提示,跳轉url
$this->CI->session->set_flashdata(‘info‘, $acl_info[$role_name][‘info‘]);
redirect($acl_info[$role_name][‘return_url‘]);
}
}
}