9
11

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

POSTでリダイレクトしたい

Posted at

POSTでリダイレクトしたかったので調べてみた。
結論としてはリダイレクトというよりも、別関数呼び出し的な感じ

はじまり

①LaravelのvalidationエラーはGETでリダイレクトするらしい

Controller
          $this->throwValidationException(
              $request, $validator
          );

②そんなgetURL無いよエラー

やりたいのはGETじゃなくてPOSTなんだよ!

やり方

呼び出したいビューに繋がる関数を呼ぶ。
ただし、そのままではビューを呼び出せないのでview(関数の戻り値)をreturnする

    //【①呼び出したい関数】
    public function postDetail(Request $request)
    {
      $req = $request->all();

      //(途中省略)

      return view('after.detail')->with('question_title' , $ank->title)
    }

    //【②リダイレクトが発生する関数】
    public function postAfter(Request $request)
    {
      $req = $request->all();
      $validator = $this->validator($request->all(), $validationArray);
      if ($validator->fails()) {
        $test1 = $this->postDetail($request);
        return $test1;
      }

      //(以降、正常時の処理)

    }
$test
View {#303 ▼
  #factory: Factory {#203 ▶}
  #engine: CompilerEngine {#302 ▶}
  #view: "after.detail"
  #data: array:7 [▶]
  #path: "/xxxx/resources/views/after/detail.blade.php"
}
9
11
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
9
11

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?