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

后端 未结 10 1873
终归单人心
终归单人心 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: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 
    }
    

提交回复
热议问题