2011-12-22 15 views
0

のコントローラのすべてのメソッドをラップ、私はIwrapperを使用してUserControllerでのすべてのメソッドをラップしたいコントローラが凹部の凹部

/** 

* !RespondsWith Layouts 

* !Prefix user/ 

*/ 

class UserController extends Controller 

{ 
...... 
} 

を持っています。私はIWrapperを使って通常のクラスのメソッドをラップする方法を知っています。しかし、コントローラの場合、UserControllerがインスタンス化されておらず、そのメソッドがリセスコントローラによって自動的に呼び出されるため、私はそれを行うことができません。

答えて

1

注釈を使用して、コントローラクラスにラッパーを追加できます。例えば、私はコントローラ「NAS」を持っている

/** 
* !RespondsWith Json,CSV 
* !Prefix nas/ 
*/ 
class NasController extends Controller { 

    /** 
     * !Route GET 
     * !VerifyPermission Module: data, Permission: read, UnauthorizedAction: noEntry 
     */ 
     function index() { 
     } 
} 

VerifyPermissionアノテーションは、次にあなたがVerifyPermissionWrapper、標準的な方法は、あなたの周りにラップされます作成することができます拡大方法

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; 
    } 
} 

にラッパーを追加します(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}'); 
    } 
} 
関連する問題