LoginSignup
0
0
記事投稿キャンペーン 「PHP強化月間」

Laravel Requestで403 Forbiddenエラーが発生

Last updated at Posted at 2023-10-06

初心者SEの備忘録。。

[発生状況]
Laravelにて、、
実行すると403 Forbiddenエラーが発生。
どうやら、バリデーションを行うために作成したFormRequestを使うとエラーが出るみたい。

app\Http\Controllers\TestController.php
public function update(Int $id, TestRequest $request) 
//↑引数が、Request $requestだとエラーは出ない、TestRequest $requestだとエラーが出る
    {
        return $this->TestService->updateTestInfo($id, $request);
    }

Requestファイルは、コマンドで作成したもの。特に変なところはない。

php artisan make:request TestRequest

と思ったら、ここに原因があったーー!

[解決方法]
作成したRequestファイル内のauthorize()のreturnをfalse⇒trueにする。

app\Http\Requests\TestRequest.php
    /**
     * Determine if the user is authorized to make this request.
     */
    public function authorize(): bool
    {
        return true; //ここをfalse⇒trueに
    }

authorize():フォームリクエストの利用が許可されているかどうかを示すメソッド。
returnでtrueだと許可、falseだと未許可ということ、みたい。

以上。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0