How to change default redirect URL of Laravel 5 Auth filter?

后端 未结 10 1874
终归单人心
终归单人心 2020-12-04 20:17

By default if I am not logged and I try visit this in browser:

http://localhost:8000/home

It redirect me to http://localhost:8000/aut

相关标签:
10条回答
  • 2020-12-04 20:30

    EDIT: On Laravel 5.1, simply add protected $redirectPath = '/url/you/want'; to AuthController would do the trick.

    REFER : http://laravel.com/docs/5.1/authentication#included-authenticating


    On Laravel 5.1, it is completely moved to another middleware named RedirectIfAuthenticated.php under App\Http\Middleware

    public function handle($request, Closure $next)
    
    {
        if ($this->auth->check()) {
            return redirect('/'); //change this part to anywhere you wish to be redirected to
        }
    
        return $next($request);
    }
    

    Hope it helps.

    0 讨论(0)
  • 2020-12-04 20:30

    Since your other question was marked as duplicate..I will try to answer it here..

    First you need to change your route like

    <?php
    Route::get(config('constants.cms_path') . '/login', [
        'as' => 'login',
        'uses' => 'Auth\AuthController@getLogin'
    ]);
    

    In your blade..make sure you use named route in the Login url link like

    {{ route('login') }}
    

    In Middleware/Authenticate.php change the redirect guest to

    return redirect()->guest(config('constants.cms_path') . '/login');
    
    0 讨论(0)
  • 2020-12-04 20:31

    Just to extend @ultimate's answer:

    1. You need to modify App\Http\Middleware\Authenticate::handle() method and change auth/login to /login.
    2. Than you need to add $loginPath property to your \App\Http\Controllers\Auth\AuthController class. Why? See Laravel source.

    In result you'll have this in your middleware:

    namespace App\Http\Middleware;
    class Authenticate {
            /**
         * Handle an incoming request.
         *
         * @param  \Illuminate\Http\Request  $request
         * @param  \Closure  $next
         * @return mixed
         */
        public function handle($request, Closure $next)
        {
            if ($this->auth->guest())
            {
                if ($request->ajax())
                {
                    return response('Unauthorized.', 401);
                }
                else
                {
                    return redirect()->guest('/login'); // <--- note this
                }
            }
    
            return $next($request);
        }
    }
    

    And this in your AuthController:

    namespace App\Http\Controllers\Auth;
    class AuthController extends Controller
    {
        protected $loginPath = '/login'; // <--- note this
    
        // ... other properties, constructor, traits, etc 
    }
    
    0 讨论(0)
  • 2020-12-04 20:32

    I wanted to do the same thing in Laravel 5.5. Handling authentication has moved to Illuminate\Auth\Middleware\Authenticate which throws an Illuminate\Auth\AuthenticationException.

    That exception is handled in Illuminate\Foundation\Exceptions\Hander.php, but you don't want to change the original vendor files, so you can overwrite it with your own project files by adding it to App\Exceptions\Handler.php.

    To do this, add the following to the top of the Handler class in App\Exceptions\Handler.php:

    use Illuminate\Auth\AuthenticationException;
    

    And then add the following method, editing as necessary:

    /**
     * Convert an authentication exception into an unauthenticated response.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Illuminate\Auth\AuthenticationException  $exception
     * @return \Illuminate\Http\Response
     */
    protected function unauthenticated($request, AuthenticationException $exception)
    {
        if ($request->expectsJson()) {
            return response()->json(['error' => 'Unauthenticated.'], 401);
        }
    
        return redirect()->guest('login'); //<----- Change this
    }
    

    Just change return redirect()->guest('login'); to return redirect()->guest(route('auth.login')); or anything else.

    I wanted to write this down because it took me more than 5 minutes to figure it out. Please drop me a line if you happened to find this in the docs because I couldn't.

    0 讨论(0)
  • 2020-12-04 20:34

    Authentication checks are made using middleware in Laravel 5.

    And the middleware for auth is App\Http\Middleware\Authenticate.

    So, you can change it in handle method of the middleware.

    0 讨论(0)
  • 2020-12-04 20:35

    This is Laravel 5.4 Solution:

    There is a new unauthenticated() method in app/Exceptions/Handler.php which handles unauthenticated users and redirects to login path.

    So change

    return redirect()->guest('login');
    

    to

    return redirect()->guest('auth/login');
    
    0 讨论(0)
提交回复
热议问题