我想在笨使用_remap方法來創建一個全球性的認證。下面是用於訪問控制器/方法的網站的條件:有什麼方法可以簡化codeigniter中的全局認證?
- 方法必須存在。
- 某些控制器只能在用戶/管理員登錄時才能訪問。
- 某些控制器只能由管理員訪問。
_remap方法寫在MY_Controller中,它將繼承到所有控制器。這裏是我的代碼:
protected $must_login;
protected $must_admin;
public function _remap($method, $param = array())
{
# Check if method exist
if (method_exists($this, $method))
{
# Check whether the user has to be login to access it
if ($this->must_login)
{
# Check whether the user has logged in
if ($this->auth->is_loggedin())
{
# Check whether it has to be admin to access it
if ($this->must_admin)
{
# Check whether it is admin
if ($this->auth->is_admin())
{
# Run the method
return call_user_func_array(array($this, $method), $param);
}
else
{
# Redirecting to login form
$this->go('auth');
}
}
else
{
return call_user_func_array(array($this, $method), $param);
}
}
else
{
$this->go('auth');
}
}
else
{
return call_user_func_array(array($this, $method), $param);
}
}
else
{
$this->go('auth');
}
}
代碼工作,但我覺得它可以簡化。我嘗試過,但總是以無限重定向結束。有什麼方法可以簡單地使用這種方法嗎?
預先感謝您。
我認爲這將是很容易通過擴展是CI_Controller來實現。在這裏的例子無處不在,並在網上如何做到這一點。 CI已檢查有效的控制器和控制器/方法,如果未找到,將會出現錯誤404。你可以利用'$ route ['404_override']'完成等同於'$ this-> go('auth');'當CI已經有一個完全可以接受的'router'時,爲什麼創建一個'go'類深思熟慮?如果你只是利用現有的框架結構,這將會更加簡化。 – DFriend
我同意,我認爲,儘管它可行,但整個方法並不是最可靠,穩健或可維護的方法,以實現實際上相對微不足道的事情 - 在調用特定控制器時,用戶是否允許訪問。這種認證可以在一條線上實現,更重要的是,這種認證的操作可以以更透明的方式進行調整。恕我直言,這是重映射功能的過度使用,完全沒有必要。再次,恕我直言,重映射是邊緣情況和情景,解決方法和fudges的後備。我個人會避免使用它。 – PaulD