如何解决InvalidArgumentException路由[登录]未在Laravel 5.6中定义

时间:2018-04-25 12:27:53

标签: laravel login routes invalidargumentexception

我收到 InvalidArgumentException Route [login] not defined 错误:

  

C:\ XAMPP \ htdocs中\购物\厂商\ laravel \框架\ SRC \照亮\路由\ UrlGenerator.php

    /**
     * Get the URL to a named route.
     *
     * @param  string  $name
     * @param  mixed   $parameters
     * @param  bool  $absolute
     * @return string
     *
     * @throws \InvalidArgumentException
     */
    public function route($name, $parameters = [], $absolute = true)
    {
        if (! is_null($route = $this->routes->getByName($name))) {
            return $this->toRoute($route, $parameters, $absolute);
        }

        throw new InvalidArgumentException("Route [{$name}] not defined.");
    }

    /**
     * Get the URL for a given route instance.
     *
     * @param  \Illuminate\Routing\Route  $route
     * @param  mixed  $parameters
     * @param  bool   $absolute
     * @return string
     *
     * @throws \Illuminate\Routing\Exceptions\UrlGenerationException

这是我的路线:

Route::group(['middleware' => ['auth']],function(){
    route::get('/admin/dashboard','AdminController@dashboard');
});

这是我的方法:

public function handle($request, Closure $next, $guard = null)
{
    if (Auth::guard($guard)->check()) {
        return redirect('/home');
    }
    else{
        return redirect()->action('AdminController@login')->with('success','please login to access');
    }

    return $next($request);
}

4 个答案:

答案 0 :(得分:4)

Laravel具有php artisan make:auth功能,如果您在终端中运行该功能,它将为您的应用程序生成auth脚手架。

请查看此文档https://laravel.com/docs/5.6/authentication

答案 1 :(得分:2)

在你的laravel应用程序中运行php artisan make:auth来支持身份验证。

答案 2 :(得分:2)

清除可能有用的视图缓存

答案 3 :(得分:0)

在exception / handler.php上添加新功能

protected function unauthenticated($request, AuthenticationException $exception){
  if($request->expectsJson()){
    return response()->json(['error' => 'Unauthenticated.'], 401);
  }

  return redirect('/home');
}