CodeIgniter4でコントローラ実行の前後に指定したイベントを実行する方法
CodeIgniter4で特定の条件の場合は指定したページにリダイレクトさせたいということがあった。条件に該当するページは複数あるので「条件に一致していればリダイレクト」という処理は一纏めにしたいがController内で別メソッドを呼び出してリダイレクトしようとしても正常に動作しない。その辺りの対応方法をメモ。
目次
リファレンス
コントローラフィルター
https://ci-trans-jp.gitlab.io/user_guide_4_jp/general/filters.html
ソースコード
/app/Filters/TestFilter.php(フィルタの内容)
以下ファイルを新規作成してアップロードする。
namespace App\Filters;
use CodeIgniter\Filters\FilterInterface;
use CodeIgniter\HTTP\RequestInterface;
use CodeIgniter\HTTP\ResponseInterface;
class TestFilter implements FilterInterface
{
public function before(RequestInterface $request, $arguments = null)
{
if( 特定条件の場合 ){
return redirect()->to('/path/to/file');
}
}
public function after(RequestInterface $request, ResponseInterface $response, $arguments = null)
{
}
}
/app/Config/Filters.php(フィルタ設定)
以下の通り変更する。
#変更前
class Filters extends BaseConfig
{
/**
* Configures aliases for Filter classes to
* make reading things nicer and simpler.
*
* @var array<string, class-string|list<class-string>> [filter_name => classname]
* or [filter_name => [classname1, classname2, ...]]
*/
public array $aliases = [
'csrf' => CSRF::class,
'toolbar' => DebugToolbar::class,
'honeypot' => Honeypot::class,
'invalidchars' => InvalidChars::class,
'secureheaders' => SecureHeaders::class,
];
#変更後
use \App\Filters\TestFilter;
class Filters extends BaseConfig
{
/**
* Configures aliases for Filter classes to
* make reading things nicer and simpler.
*
* @var array<string, class-string|list<class-string>> [filter_name => classname]
* or [filter_name => [classname1, classname2, ...]]
*/
public array $aliases = [
'csrf' => CSRF::class,
'toolbar' => DebugToolbar::class,
'honeypot' => Honeypot::class,
'invalidchars' => InvalidChars::class,
'secureheaders' => SecureHeaders::class,
'TestFilter' => TestFilter::class,
];
/app/Config/Routes.php(ルーティング設定)
以下を追記する。
$routes->add('/path', 'Test_Controller::index', ['filter' => 'TestFilter']);
「https://test.com/codeigniter4/path」をブラウザから開くと/app/Filters/TestFilter.phpで指定したイベントが実行される。
今回だと特定の条件の場合に/path/to/fileにリダイレクトする、という形になる。
注意点等
before / after
/app/Filters/TestFilter.phpのbeforeがコントローラ実行前に実行され、afterがコントローラー実行後に実行される。
before / afterのどちらかが不要でも実装する必要あり(以下公式案内)。
それぞれコントローラの前後に実行されます。あなたのクラスには両方を実装しなければなりませんが、不要ならメソッドの中身を空にしておくとよいです。
公式案内の/app/Filters/TestFilter.phpをコピペするとエラーになる
公式案内通り以下をコピペしてもエラーとなった。
<?php namespace App\Filters;
use CodeIgniter\HTTP\RequestInterface;
use CodeIgniter\HTTP\ResponseInterface;
use CodeIgniter\Filters\FilterInterface;
class MyFilter implements FilterInterface
{
public function before(RequestInterface $request)
{
// ここで何かをする
}
//--------------------------------------------------------------------
public function after(RequestInterface $request, ResponseInterface $response)
{
// ここで何かをする
}
}
エラーメッセージは以下の通り。
Fatal error: Declaration of App\Filters\xxxxxxx::before(CodeIgniter\HTTP\RequestInterface $request) must be compatible with CodeIgniter\Filters\FilterInterface::before(CodeIgniter\HTTP\RequestInterface $request, $arguments = null) in /app/Filters/xxxxxxx.php on line 10 Declaration of App\Filters\xxxxxxx::before(CodeIgniter\HTTP\RequestInterface $request) must be compatible with CodeIgniter\Filters\FilterInterface::before(CodeIgniter\HTTP\RequestInterface $request, $arguments = null)
引数が足りないようでbefore / afterどちらも「$arguments = null」を追加することで改善された。
関連記事
-
-
CodeIgniter4.4.4のインストールからHello Worldページ表示まで行う方法
以前CodeIgniter3を勉強したことがあったが新バージョンの4系がリリース ...
-
-
CodeIgniter4の各種設定項目のまとめ記事
CodeIgniter4.4.4の各種設定項目を後から確認できるようまとめておき ...
-
-
CodeIgniter4 & jQueryを用いて同一サイト内でajax通信する方法
CodeIgniter4.4.4 & jQueryを用いて同一サイト内で ...
-
-
CodeIgniter3でCronを実行する方法
CodeIgniter3系で特定の処理をCronで自動実行したかった。以下に設定 ...
-
-
CodeIgniter4でCronを実行する方法
CodeIgniter4.4.4で特定の処理をCronで自動実行させたい。以下に ...