must be of the type integer, string given のエラー

以下のエラーが出た。

 

production.ERROR: Argument 4 passed to App\Http\Controllers\xxxxx\xxxxxController::main() must be of the type integer, string given, called in /var/www/xxxxxxxxx/vendor/laravel/framework/src/Illuminate/Routing/Controller.php on line 54 {"exception":"[object] (TypeError(code: 0): Argument 4 passed to App\\Http\\Controllers\\xxxxx\\xxxxxController::main() must be of the type integer, string given, called in /var/www/xxxxxxxxx/vendor/laravel/framework/src/Illuminate/Routing/Controller.php on line 54 at /var/www/xxxxxxxxx/app/Http/Controllers/xxxxx/xxxxxController.php:37)
[stacktrace]
#0 /var/www/xxxxxxxxx/vendor/laravel/framework/src/Illuminate/Routing/Controller.php(54): App\\Http\\Controllers\\xxxxx\\xxxxxController->main(Object(Illuminate\\Http\\Request), 'zzzzzzzzzzzz', 'yyyyyyyyyy', 'DisplayObject', 'event.KeyUp.htm...')

 

 

原因は、

public function main(Request $aaaaa, string $bbbb, string $ccccc, int $dddddd)

の $dddddd に string が渡されたから。

$ddddddはURLの入力値を使ってるから、string が入ってくる可能性が排除できない。

というわけで、以下で対処しようと思う

public function main(Request $aaaaa, string $bbbb, string $ccccc, $dddddd)

$dddddd= (int) $dddddd;

 

 

【PHP】タイプヒンティング - Qiita

パラメータに何を渡したら良いかすぐにわかるので可読性が上がる。
パラメータの型が保障される。パラメータの型が違うことが原因の不具合はすぐに検出できる。簡易的なパラメータチェック。