Help us understand the problem. What is going on with this article?

Laravel 5.3の認証関連機能で新規登録・ログイン・ログアウト後のリダイレクト先を指定する方法

More than 3 years have passed since last update.

やりたいこと

  • php artisan make:authで認証関連機能を生成すると、新規登録・ログイン後は「/home」へ、ログアウト後は「/」へリダイレクトされるが、これを変更したい。

前提

* php artisan make:authで作成した認証関連機能であること

参考

設定方法

新規登録後のリダイレクト先を変更する

  • RegisterControllerの$redirectToプロパティを変更する

    app/Http/Controller/Auth/RegisterController.php
    <?php
    
    namespace App\Http\Controllers\Auth;
    
    //...
    
    class RegisterController extends Controller
    {
    
        //...
    
        protected $redirectTo = '/redirect/path/to/after/regist/';  //ここを変更   
    
        //...
    
        public function __construct()
        {
            //環境別に切り替えるロジックなどが必要な場合、__construct内で指定してもOK
            $this->redirectTo = your_function('/redirect/path/to/after/regist/');
    
            // ...
        }
    
        //...
    }
    
    

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

  • LoginControllerの$redirectToプロパティを変更する

    app/Http/Controller/Auth/LoginController.php
    <?php
    
    namespace App\Http\Controllers\Auth;
    
    //...
    
    class LoginController extends Controller
    {
    
        //...
    
        protected $redirectTo = '/redirect/path/to/after/regist/';  //ここを変更   
    
        //...
    
        public function __construct()
        {
            //環境別に切り替えるロジックなどが必要な場合、__construct内で指定してもOK
            $this->redirectTo = your_function('/redirect/path/to/after/regist/');
    
            // ...
        }
    
        //...
    }
    
    

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

  • LoginControllerでlogout()メソッドをオーバーライトする

    app/Http/Controller/Auth/LoginController.php
    <?php
    
    namespace App\Http\Controllers\Auth;
    
    // ...
    
    use Illuminate\Http\Request;
    
    class LoginController extends Controller
    {
        //...
    
        //AuthenticatesUsersトレイトで定義されたlogout()をオーバーライト。
        //最終行のreturn redirect();の内容を変更する。
        public function logout(Request $request)
        {
            $this->guard()->logout();
    
            $request->session()->flush();
    
            $request->session()->regenerate();
    
            return redirect('/redirect/path/to/after/regist/');
        }
    }
    
    
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
No comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
ユーザーは見つかりませんでした