2
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 2021-01-27

はじめに

laravelで認証機能をmake:authで作成する際にリダイレクト先が設定されています。また、middlewareでログインしないと任意のページに遷移できないように設定済です。
今後新たなwebアプリケーションを作成する可能性が高いので任意のページを変更してみたいと思います。

やってみること

ログインすると任意のページにリダイレクトを設定する

投稿者の環境

MacBook Pro (13-inch, 2019, Two Thunderbolt 3 ports)
macOS Big Sur バージョン11.0.1
PHP 7.3.23
Laravel Framework 6.20.7

リダイレクト先を変更してみよう

ステップ1

HTTP->Middleware->RedirectIfAuthenticated.phpを編集する。
フォルダの構成はこのようになっている。“スクリーンショット” 2021-01-27 18.12.25.jpg

RedirectIfAuthenticated.php
<?php

namespace App\Http\Middleware;

use Closure;
use Illuminate\Support\Facades\Auth;

class RedirectIfAuthenticated
{
    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @param  string|null  $guard
     * @return mixed
     */
    public function handle($request, Closure $next, $guard = null)
    {
        if (Auth::guard($guard)->check()) {
            return redirect('/home'); //こちらを変更する
        }

        return $next($request);
    }
}

コードの編集

RedirectIfAuthenticated.php
return redirect('/home');
//以下のように変更する
return redirect('online_reviews/hospital_list/index');

ステップ2

Authフォルダ->LoginController.phpを編集する。

LoginController.php
<?php

namespace App\Http\Controllers\Auth;

use App\Http\Controllers\Controller;
use Illuminate\Foundation\Auth\AuthenticatesUsers;

class LoginController extends Controller
{
    /*
    |--------------------------------------------------------------------------
    | Login Controller
    |--------------------------------------------------------------------------
    |
    | This controller handles authenticating users for the application and
    | redirecting them to your home screen. The controller uses a trait
    | to conveniently provide its functionality to your applications.
    |
    */

    use AuthenticatesUsers;

    /**
     * Where to redirect users after login.
     *
     * @var string
     */
    protected $redirectTo = '/home';//ここを編集する。

    /**
     * Create a new controller instance.
     *
     * @return void
     */
    public function __construct()
    {
        $this->middleware('guest')->except('logout');
    }
}

コードの編集

LoginController.php
protected $redirectTo = '/home';
//以下のように変更する
protected $redirectTo = 'online_reviews/hospital_list/index';

以上でコードの編集は終了です。無事にリダレクト先が変更されましたか?

解説

ルーティングの設定でweb.phpを見てみます。一番上に記述しているgroup``'prefix'をしているため'online_reviews'を記述する必要がありません。その反面、リダイレクト先を設定するにはこの記述を記述しないとエラーが発生します。
groupとprefixに関してはこちらのページを参考にして下さい。

web.php
Route::group(['prefix' => 'online_reviews','middleware' => 'auth'], function(){
  Route::get('hospital_list/index','OnlineReviewsController@index')->name('online_reviews.hospital_list'); 
 //以下省略

最後に

初めての試みだったので試行錯誤の連続・・・
認証機能は自動で作成できる反面、カスタマイズが難しい・・・
ログイン機能をもっと充実させるようにがんばります!!

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