我想從以下行獲得當前控制器的名字:路由器錯誤與CI
$this->router->fetch_class();
我發現線在這裏:
https://gist.github.com/svizion/2325988
這是我想要使用它在一個鉤子。
<?php
if (!defined('BASEPATH'))
exit('No direct script access allowed');
class Sys_prescript {
public function is_logged_in()
{
$public_access = array('login', 'registration');
if (!in_array($this->router->fetch_class(), $public_access))
{
$user_id = $this -> session -> userdata('user_id');
if (($user_id == FALSE) || (is_numeric($user_id) == FALSE) && (strlen($user_id) < 5))
{
redirect('login');
}
}
}
}
唯一的問題是我的「M收到以下錯誤
A PHP Error was encountered
Severity: Notice
Message: Undefined property: Sys_prescript::$router
Filename: controllers/sys_prescript.php
Line Number: 9
Fatal error: Call to a member function fetch_class() on a non-object in...
使用hook而不是檢查路由器.. pre_system掛鉤實現這個 – Sundar
這是一個鉤子。 – user2576961
如果鉤子是系統級別,那麼你可以從$ _SERVER ['REQUEST_URI']變量獲得URL詳細信息,這很簡單,爲什麼你想嘗試在路由器類 – Sundar