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

【Laravel】ログイン後、ログアウト後の遷移先指定方法

Posted at

はじめに

ログイン、ログアウトに分けて解説していきます。

ログインのリダイレクト先を変更

方法1:RouteServiceProvider.php で一括管理

/app/Http/Controllers/Auth/AuthenticatedSessionController.php
# store() メソッド
return redirect()->intended(RouteServiceProvider::HOME);

return redirect()->intended(RouteServiceProvider::HOME)の解説

Laravelのログイン後に**「元々アクセスしようとしていたページ」**があればそこに戻し、なければ RouteServiceProvider::HOME にリダイレクトする。

状況 遷移先
🔙 ログイン前に auth が必要なページにアクセスしていた そのページへ戻る(=intendedの意味)
⚙ 特にアクセス元がない RouteServiceProvider::HOME にリダイレクトされる

RouteServiceProvider::HOME の場所と値

app/Providers/RouteServiceProvider.php
# 一番上
public const HOME = '/dashboard';

つまり、最初の状態では「/dashboard に飛ぶ」という設定になっている。

方法2:AuthenticatedSessionController.php で個別に変更

/Http/Controllers/Auth/AuthenticatedSessionController.php
# store() メソッドのリダイレクト部分を以下のように変更
# 編集前(方法1を済んでいたらこのままでOK)
return redirect()->intended(RouteServiceProvider::HOME);
# 編集後
return redirect()->intended('/'); // または任意のURL

ここでは RouteServiceProvider::HOME を使っていて、前述の HOME 定数と連動しています。
return redirect()->intended(...) = 「元々アクセスしようとしていたページに戻る」処理

ログアウト後のリダイレクト先を変更

/Controllers/Auth/AuthenticatedSessionController.php
# destroy() メソッドの最後にあるリダイレクト先を変更
return redirect('/admin'); // 任意のURLに変更可能

✅ まとめ

処理 変更箇所
ログイン後 「AuthenticatedSessionController@store」 or 「RouteServiceProvider::HOME」
ログアウト後 AuthenticatedSessionController@destroy
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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?