LoginSignup
0
1

More than 3 years have passed since last update.

Laravel ログインしていない状態でもURLを直接入れると見れてしまう現象を一行で解決させる方法(Middleware)

Last updated at Posted at 2020-08-24

未ログインユーザーに記事投稿画面を表示しないようにするためには、

web.php
Route::resource('/articles', 'ArticleController')->except(['index'])->middleware('auth'); 

上記にmiddlewareを入れるだけでログインしていない状態であれば、

ログイン画面に遷移されます。

.
└──laravel
    └── app
        └── Http
            └── Middleware
                └── Authenticate.php
Authenticate

<?php

namespace App\Http\Middleware;

use Illuminate\Auth\Middleware\Authenticate as Middleware;

class Authenticate extends Middleware
{
    /**
     * Get the path the user should be redirected to when they are not authenticated.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return string
     */
    protected function redirectTo($request)
    {
        if (! $request->expectsJson()) {
            return route('login');
        }
    }
}

loginのところをいじれば遷移されるところを、アレンジすることができます。

以上です:relaxed:

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