このページの2つのバージョン間の差分を表示します。
両方とも前のリビジョン 前のリビジョン 次のリビジョン | 前のリビジョン | ||
apricot:ext:interceptor [2020/05/25 20:49] y2sunlight |
apricot:ext:interceptor [2020/06/08 16:25] (現在) tanaka [Apricot インターセプター] |
||
---|---|---|---|
行 22: | 行 22: | ||
インターセプター とはアクションの前処理の事です。ミドルウェアと同じでリクエストを中断してレスポンスオブジェクトを生成することもできますが、アクションの後処理はできません。これを図示すると以下のようになります。 | インターセプター とはアクションの前処理の事です。ミドルウェアと同じでリクエストを中断してレスポンスオブジェクトを生成することもできますが、アクションの後処理はできません。これを図示すると以下のようになります。 | ||
- | Middleware | + | === インターセプター構造 === |
- | ┌──────────┐ | + | |
- | | | | + | {{: |
- | | + | |
- | [Request ] --> | -------> | --> |--|--------> | + | |
- | | + | |
- | | + | |
- | | + | |
- | [Response] <-- | < | + | |
- | | + | |
- | | + | |
上図から分かるようにミドルウェアパイプラインから見ると、インターセプターはアクションに含まれます。ミドルウェアとの一番の違いは、ミドルウェアは基本的に全てのコントローラを対象としているのに対し、インターセプターは、各コントローラで独自に設定ができるという点です。 | 上図から分かるようにミドルウェアパイプラインから見ると、インターセプターはアクションに含まれます。ミドルウェアとの一番の違いは、ミドルウェアは基本的に全てのコントローラを対象としているのに対し、インターセプターは、各コントローラで独自に設定ができるという点です。 | ||
行 69: | 行 61: | ||
protected function intercept($actionName, | protected function intercept($actionName, | ||
{ | { | ||
- | $interceptor_arr | + | |
- | if (!array_key_exists($actionName, | + | |
{ | { | ||
- | $this-> | + | |
+ | | ||
} | } | ||
- | $this-> | ||
} | } | ||
行 100: | 行 91: | ||
// 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: | 行 132: | ||
* '' | * '' | ||
+ | * リクエストされているアクション(Application:: | ||
* アクションにインターセプターを追加します。 | * アクションにインターセプターを追加します。 | ||
- | * BaseControllerは、インターセプターを配列( < | + | * BaseControllerは、インターセプターを配列( < |
* '' | * '' | ||
行 158: | 行 147: | ||
===== インターセプターの使用 ===== | ===== インターセプターの使用 ===== | ||
- | インターセプターの登録は、コントローラーのコンストラクタで '' | + | インターセプターの登録は、コントローラーのコンストラクタで '' |
<code php> | <code php> | ||
行 176: | 行 165: | ||
</ | </ | ||
- | インターセプターに渡される引数は、第1引数に、コントローラのインスタンスが、その後にアクションと同じの引数が続きます。 | + | インターセプターに渡される引数は、第1引数に、コントローラのインスタンスが、その後にアクションと同じの引数が続きます。また、インターセプターはレスポンスオブジェクトを返して以降のアクションを中止することができます。 |
- | '' | + | インターセプターにメソッド型を使用する場合は、以下のように < |
<code php> | <code php> | ||
行 216: | 行 205: | ||
}); | }); | ||
</ | </ | ||
- | - login() メソッド内からバリデーション( validate() ) の呼び出しを削除します。 | + | - login() メソッド内からバリデーション( validate() ) の呼び出し部分を削除します。 |
- validate() を削除します。 | - validate() を削除します。 | ||
- | 以下に修正後の AuthController.php を示します。 | + | 以下に修正後の最終的な |
{{fa> | {{fa> | ||
行 258: | 行 247: | ||
}); | }); | ||
} | } | ||
- | | + | |
+ | /** | ||
+ | | ||
+ | * @return \Core\Foundation\Response | ||
+ | */ | ||
+ | public function showForm() | ||
+ | { | ||
+ | if (AuthUser:: | ||
+ | { | ||
+ | // 認証済ならトップ画面表示 | ||
+ | return redirect(route('' | ||
+ | } | ||
+ | |||
+ | if (AuthUser:: | ||
+ | { | ||
+ | // 自動認証できたらトップ画面表示 | ||
+ | return redirect(route('' | ||
+ | } | ||
+ | |||
+ | return render(' | ||
+ | } | ||
/** | /** | ||
行 278: | 行 287: | ||
return redirect(AuthUser:: | return redirect(AuthUser:: | ||
} | } | ||
- | | + | |
+ | /** | ||
+ | | ||
+ | * @return \Core\Foundation\Response | ||
+ | */ | ||
+ | public function logout() | ||
+ | { | ||
+ | // セッションの破棄 | ||
+ | AuthUser:: | ||
+ | |||
+ | // ログイン画面表示 | ||
+ | return redirect(route(" | ||
+ | } | ||
} | } | ||
</ | </ | ||
行 329: | 行 350: | ||
// モデル | // モデル | ||
$this-> | $this-> | ||
+ | |||
+ | // インターセプター登録 | ||
+ | $this-> | ||
+ | $this-> | ||
// トランザクションアクション登録 | // トランザクションアクション登録 | ||
行 362: | 行 387: | ||
public function insert() | public function insert() | ||
{ | { | ||
- | // バリデーション | ||
- | $response = (new Interceptors\UserInterceptor())-> | ||
- | if ($response instanceof \Core\Foundation\Response) | ||
- | { | ||
- | return $response; | ||
- | } | ||
- | |||
$inputs = Input:: | $inputs = Input:: | ||
行 410: | 行 428: | ||
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:: | ||