【问题标题】:Wrapping all methods of a controller in Recess在 Recess 中包装控制器的所有方法
【发布时间】:2011-12-22 07:05:58
【问题描述】:

课间休息时,我有一个控制器

/**

 * !RespondsWith Layouts

 * !Prefix user/

 */

class UserController extends Controller 

{
......
}

我想使用 Iwrapper 包装 UserController 的所有方法。我知道如何使用 IWrapper 包装普通类的方法。但是在控制器的情况下,我无法做到这一点,因为 UserController 没有被实例化,并且它的方法由凹槽控制器自动调用。

【问题讨论】:

    标签: php recess


    【解决方案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}');
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-02-18
      • 2018-12-05
      • 2015-12-07
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多