1
0

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 3 years have passed since last update.

Laravelのリダイレクト処理を扱うインスタンス【個人的なお勉強アウトプット】

Last updated at Posted at 2022-03-28

参考図書

参考記事

Laravelにはリダイレクト処理を扱う2つのインスタンスがある。

  • Illuminate\Http\RedirectResponseクラスのインスタンス
  • Illuminate\routing\Redirectorクラスのインスタンス

Illuminate\Http\RedirectResponseクラスのインスタンス

メソッドチェーンとして使える($this自身を戻り値とする)ものだけ。

入力データを付加する

$response->withInput()
送られてきたフォームの値を付加してリダイレクト

バリデータのエラーを付加する

$response->withErrors(MessageProvider)
エラーメッセージを付加してリダイレクトするもの。
引数には、MessageProviderというインターフェイスを用意する?

クッキーを付与する

$response->withCookie('Cookie配列')
クッキーデータを複数付加してリダイレクトするもの。

Illuminate\routing\Redirectorクラスのインスタンス

ルートおよびアクションを指定

redirect()->route(ルート名,配列)
redirect()->action(アクションの指定,配列)
routeはweb.phpにルート情報として記述したものを指定。
actionはHelloController@indexのようにコントローラーのアクションを指定

JSONデータを返す

redirect()->json(テキスト)
指定のJSONデータを出力させることができる

ファイルを返す

redirect()->download(ファイルパス)
redirect()->file(ファイルパス)
ファイルをダウンロードしたり表示するもの。

1
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
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?