このページの2つのバージョン間の差分を表示します。
両方とも前のリビジョン 前のリビジョン 次のリビジョン | 前のリビジョン 次のリビジョン 両方とも次のリビジョン | ||
apricot:ext:interceptor [2020/05/25 20:55] y2sunlight [インターセプターの使用] |
apricot:ext:interceptor [2020/06/08 16:22] tanaka [Apricot インターセプター] |
||
---|---|---|---|
行 22: | 行 22: | ||
インターセプター とはアクションの前処理の事です。ミドルウェアと同じでリクエストを中断してレスポンスオブジェクトを生成することもできますが、アクションの後処理はできません。これを図示すると以下のようになります。 | インターセプター とはアクションの前処理の事です。ミドルウェアと同じでリクエストを中断してレスポンスオブジェクトを生成することもできますが、アクションの後処理はできません。これを図示すると以下のようになります。 | ||
- | Middleware | + | {{: |
- | | + | |
- | | + | |
- | | + | |
- | [Request ] --> | -------> | --> |--|--------> | + | |
- | | + | |
- | | + | |
- | | + | |
- | [Response] <-- | < | + | |
- | | + | |
- | | + | |
上図から分かるようにミドルウェアパイプラインから見ると、インターセプターはアクションに含まれます。ミドルウェアとの一番の違いは、ミドルウェアは基本的に全てのコントローラを対象としているのに対し、インターセプターは、各コントローラで独自に設定ができるという点です。 | 上図から分かるようにミドルウェアパイプラインから見ると、インターセプターはアクションに含まれます。ミドルウェアとの一番の違いは、ミドルウェアは基本的に全てのコントローラを対象としているのに対し、インターセプターは、各コントローラで独自に設定ができるという点です。 | ||
行 69: | 行 59: | ||
protected function intercept($actionName, | protected function intercept($actionName, | ||
{ | { | ||
- | $interceptor_arr | + | |
- | if (!array_key_exists($actionName, | + | |
{ | { | ||
- | $this-> | + | |
+ | | ||
} | } | ||
- | $this-> | ||
} | } | ||
行 100: | 行 89: | ||
// Invoke Interceptor | // Invoke Interceptor | ||
- | | + | |
+ | foreach($this-> | ||
{ | { | ||
- | | + | |
- | foreach($this-> | + | |
{ | { | ||
- | | + | |
+ | $response = call_user_func_array($interceptor, | ||
+ | } | ||
+ | elseif(strpos($interceptor,' | ||
+ | { | ||
+ | // Case of Controller/ | ||
+ | list($class, | ||
+ | if (empty($class)) | ||
{ | { | ||
- | | + | $instance |
- | | + | |
} | } | ||
- | | + | |
{ | { | ||
- | | + | $class = " |
- | list($class, | + | $instance = new $class; |
- | if (empty($class)) | + | |
- | { | + | |
- | $instance = $this; | + | |
- | } | + | |
- | else | + | |
- | { | + | |
- | | + | |
- | $instance = new $class; | + | |
- | } | + | |
- | + | ||
- | // Call interceptor | + | |
- | $response = call_user_func_array(array($instance, | + | |
} | } | ||
- | if ($response instanceof \Core\Foundation\Response) | + | |
- | { | + | $response = call_user_func_array(array($instance, |
- | return $response; | + | } |
- | } | + | |
+ | | ||
+ | { | ||
+ | return $response; | ||
} | } | ||
} | } | ||
行 144: | 行 130: | ||
* '' | * '' | ||
+ | * リクエストされているアクション(Application:: | ||
* アクションにインターセプターを追加します。 | * アクションにインターセプターを追加します。 | ||
- | * BaseControllerは、インターセプターを配列( < | + | * BaseControllerは、インターセプターを配列( < |
* '' | * '' | ||
行 216: | 行 203: | ||
}); | }); | ||
</ | </ | ||
- | - login() メソッド内からバリデーション( validate() ) の呼び出しを削除します。 | + | - login() メソッド内からバリデーション( validate() ) の呼び出し部分を削除します。 |
- validate() を削除します。 | - validate() を削除します。 | ||
行 361: | 行 348: | ||
// モデル | // モデル | ||
$this-> | $this-> | ||
+ | |||
+ | // インターセプター登録 | ||
+ | $this-> | ||
+ | $this-> | ||
// トランザクションアクション登録 | // トランザクションアクション登録 | ||
行 394: | 行 385: | ||
public function insert() | public function insert() | ||
{ | { | ||
- | // バリデーション | ||
- | $response = (new Interceptors\UserInterceptor())-> | ||
- | if ($response instanceof \Core\Foundation\Response) | ||
- | { | ||
- | return $response; | ||
- | } | ||
- | |||
$inputs = Input:: | $inputs = Input:: | ||
行 442: | 行 426: | ||
public function update(int $id) | public function update(int $id) | ||
{ | { | ||
- | // バリデーション | ||
- | $response = (new Interceptors\UserInterceptor())-> | ||
- | if ($response instanceof \Core\Foundation\Response) | ||
- | { | ||
- | return $response; | ||
- | } | ||
- | |||
$inputs = Input:: | $inputs = Input:: | ||