CakePHP | beforeFilterのWarning対応方法

ControllerにbeforeFilterメソッドを追加すると、下記のWarningメッセージが表示される場合があります。

Warning (2): Declaration of App\Controller\HogeController::beforeFilter(App\Controller\Event $event) should be compatible with App\Controller\AppController::beforeFilter(Cake\Event\Event $event) [APP/Controller/LearningsController.php, line 12]
Code Context
include - APP/Controller/HogeController.php, line 12
Composer\Autoload\includeFile - ROOT/vendor/composer/ClassLoader.php, line 444
Composer\Autoload\ClassLoader::loadClass() - ROOT/vendor/composer/ClassLoader.php, line 322
spl_autoload_call - [internal], line ??
class_exists - [internal], line ??
Cake\Core\App::_classExistsInBase() - CORE/src/Core/App.php, line 152
Cake\Core\App::className() - CORE/src/Core/App.php, line 64
Cake\Http\ControllerFactory::getControllerClass() - CORE/src/Http/ControllerFactory.php, line 92
Cake\Http\ControllerFactory::create() - CORE/src/Http/ControllerFactory.php, line 37
Cake\Http\ActionDispatcher::dispatch() - CORE/src/Http/ActionDispatcher.php, line 91
Cake\Http\BaseApplication::__invoke() - CORE/src/Http/BaseApplication.php, line 235
Cake\Http\Runner::__invoke() - CORE/src/Http/Runner.php, line 65
Cake\Http\Runner::__invoke() - CORE/src/Http/Runner.php, line 65
Cake\Http\Middleware\CsrfProtectionMiddleware::__invoke() - CORE/src/Http/Middleware/CsrfProtectionMiddleware.php, line 123
Cake\Http\Runner::__invoke() - CORE/src/Http/Runner.php, line 65
Cake\Http\Runner::run() - CORE/src/Http/Runner.php, line 51
Cake\Routing\Middleware\RoutingMiddleware::__invoke() - CORE/src/Routing/Middleware/RoutingMiddleware.php, line 168

Waring対応

このWaringが表示される原因は、主に2つあります。
それぞれの対応をすると、Waringメッセージが表示されなくなります。

useの記載漏れ

下記のコードがない場合には、追記してください。

use Cake\Event\Event;

メソッドの引数不足

public function beforeFilter()
{
//個別処理
}

↓

引数を追加
public function beforeFilter(Event $event)
{
//個別処理
}

スポンサーリンク