Route [login] not defined

前端 未结 14 1889
無奈伤痛
無奈伤痛 2020-12-02 15:15

Trying to play with Laravel today for the first time. I am getting the following error when I attempt to visit localhost/project/public:

InvalidArgume

相关标签:
14条回答
  • 2020-12-02 15:57

    You're trying to redirect to a named route whose name is login, but you have no routes with that name:

    Route::post('login', [ 'as' => 'login', 'uses' => 'LoginController@do']);
    

    The 'as' portion of the second parameter defines the name of the route. The first string parameter defines its route.

    0 讨论(0)
  • 2020-12-02 15:59

    Replace in your views (blade files) all

    {{route('/')}} ----- by ----> {{url('/')}}

    0 讨论(0)
  • 2020-12-02 15:59
    Route::post('login', 'LoginController@login')->name('login')
    

    works very well and it is clean and self-explanatory

    0 讨论(0)
  • 2020-12-02 16:00

    In app\Exceptions\Handler.php

    protected function unauthenticated($request, AuthenticationException $exception)
    {
        if ($request->expectsJson()) {
            return response()->json(['error' => 'Unauthenticated.'], 401);
        }
    
        return redirect()->guest(route('auth.login'));
    }
    
    0 讨论(0)
  • 2020-12-02 16:01

    You need to add the following line to your web.php routes file:

    Auth::routes();
    

    In case you have custom auth routes, make sure you /login route has 'as' => 'login'

    0 讨论(0)
  • 2020-12-02 16:02

    Laravel ^5.7

    The Authenticate Middleware

    Laravel ^5.7 includes new middleware to handle and redirect unauthenticated users.

    It works well with "web" guard... of course the "login" route (or whatever you name your login route) should be defined in web.php.

    the problem is when your are using custom guard. Different guard would redirect unauthenticated users to different route.

    here's a quick workaround based on John's response (it works for me).


    app/Http/Middleware/Authenticate.php

    <?php
    
    namespace App\Http\Middleware;
    
    use Closure;
    use Illuminate\Auth\Middleware\Authenticate as Middleware;
    
    class Authenticate extends Middleware
    {
        /**
         * @var array
         */
        protected $guards = [];
    
    
    
    
        /**
         * Handle an incoming request.
         *
         * @param  \Illuminate\Http\Request  $request
         * @param  \Closure  $next
         * @param  string[]  ...$guards
         * @return mixed
         *
         * @throws \Illuminate\Auth\AuthenticationException
         */
        public function handle($request, Closure $next, ...$guards)
        {
            $this->guards = $guards;
    
            return parent::handle($request, $next, ...$guards);
        }
    
    
    
    
        /**
         * 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()) {
    
                if (in_array('admin', $this->guards)) {
    
                    return route('admin.login');
                }
    
                return route('login');
            }
        }
    }
    

    Source : Issue #26292

    0 讨论(0)
提交回复
热议问题