我正在创建休息服务。我已经完成了所有需要的方法,我想做的是一些授权。我已经创建了存储 api key 的表,我在每个方法中加载它们,并且效果很好。 我现在需要的是做一些在每个方法之前调用的 before action,这样我就不必检查用户是否在每个方法上成功授权?在简单的 CI_Controller 或 FuelPHP 中,以前可以使用公共(public)函数完成,但我不知道如何在 REST_Controller 中实现同样的事情?
提前致谢
最佳答案
这里有两个 Controller 。可能会给你一些想法
class MY_Controller extends CI_Controller
{
protected $before_filter = array();
protected $after_filter = array();
private function run_filter($who, $params=array())
{
$filter = $this->{"{$who}_filter"};
if (is_string($filter)) {
$filter = array($filter);
}
if (method_exists($this, "{$who}_filter")) {
$filter[] = "{$who}_filter";
}
foreach ($filter as $method) {
call_user_func_array(array($this, $method), $params);
}
}
public function _remap($method, $parameters)
{
if (method_exists($this, $method))
{
$this->run_filter('before', $parameters);
$return = call_user_func_array(array($this, $method),$parameters);
$this->run_filter('after', $parameters);
}else{
show_404();
}
}
}
class MY_Controller extends CI_Controller
{
public $before_filter = array('check_login');
public $after_filter = array();
private function dashboard()
{
/* other code here */
}
public function check_login()
{
/* Login checking Code here */
}
}
关于php - 行动前 phil 的休息服务器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13376594/