休息时,我有一个控制器
/**
* !RespondsWith Layouts
* !Prefix user/
*/
class UserController extends Controller
{
......
}
我想使用Iwrapper包装UserController的所有方法。我知道如何使用IWrapper包装普通类的方法。但是在控制器的情况下,我无法做到这一点,因为UserController没有实例化,其方法由凹槽控制器自动调用。
答案 0 :(得分:1)
您可以使用注释将包装器添加到控制器类。例如,我有一个控制器“nas”
/**
* !RespondsWith Json,CSV
* !Prefix nas/
*/
class NasController extends Controller {
/**
* !Route GET
* !VerifyPermission Module: data, Permission: read, UnauthorizedAction: noEntry
*/
function index() {
}
}
VerifyPermission注释将在expand方法
中添加包装器Library::import('recess.lang.Annotation');
Library::import('cirrusWorks.wrappers.VerifyPermissionWrapper');
class VerifyPermissionAnnotation extends Annotation {
protected function expand($class, $reflection, $descriptor) {
$module = $this->module;
$permission = $this->permission;
$unauthorizedAction = $this->unauthorizedaction;
$descriptor->addWrapper('serve',new VerifyPermissionWrapper($module,$permission,$unauthorizedAction, $reflection->getName()));
/* ... */
return $descriptor;
}
}
然后你可以创建VerifyPermissionWrapper,标准方法将包含在你的类方法中(before(),after(),combine())
class VerifyPermissionWrapper implements IWrapper {
function __construct($module, $permission, $action, $method) {
$this->module = $module;
$this->permission = $permission;
$this->action = $action;
$this->method = $method;
}
function before($controller, &$args) {
error_log('Before {$this->action} on {$this->method}');
}
}