Laravel Route [仪表板]未定义

时间:2018-01-08 12:58:24

标签: php laravel

第一次尝试与Laravel一起玩。我尝试访问时收到以下错误:

  

InvalidArgumentException

     

路线[仪表板]未定义。

路由/ web.php

Route::get('/', ['as' => '/', 'uses' => 'LoginController@getLogin']);
Route::post('/login', ['as' => 'login', 'uses' => 'LoginController@postLogin']);
Route::get('/logout', ['as' => 'logout', 'uses' => 'LoginController@getLogout']);

Route::group(['middleware' => ['authenticate', 'roles']], function (){
    Route::get('/dashboard', ['as' => 'dashboard', 'uses' => 'DashboardController@dashboard']);
});

LoginController.php

class LoginController extends Controller
{
    use AuthenticatesUsers;

    protected $username = 'username';
    protected $redirectTo = '/';
    protected $guard = 'web';

    public function getLogin()
    {
        if (Auth::guard('web'))
        {
            return redirect()->route('dashboard');
        }
        return view('login');
    }

    public function postLogin(Request $request)
    {
        $auth = Auth::guard('web')->attempt([
            'username' => $request->username,
            'password' => $request->password,
            'active' => 1]);
        if ($auth)
        {
            return redirect()->route('dashboard');
        }
        return redirect()->route('/');
    }

    public function getLogout()
    {
        Auth::guard('web')->logout();
        return redirect()->route('/');
    }
}

Error

3 个答案:

答案 0 :(得分:0)

试试这个:

Route::get('/dashboard','DashboardController@dashboard')->name('dashboard');

答案 1 :(得分:0)

当您使用它来命名路线时,因此您添加了两个名称dashbbaorddashboard,因为您使用了asname

Route::group(['middleware' => ['authenticate', 'roles']], function (){
    Route::get('/dashboard', ['uses' => 'DashboardController@dashboard'])->name('dashboard');
});

这将有效

答案 2 :(得分:0)

asname()。您应该使用以下两种之一:

Route::group(['middleware' => ['authenticate', 'roles']], function (){
    Route::get('/dashboard', 'DashboardController@dashboard')->name('dashboard');
});

Route::group(['middleware' => ['authenticate', 'roles']], function (){
    Route::get('/dashboard', [
    'as' => 'dashboard', 
    'uses' => 'DashboardController@dashboard']);
});

之后,您使用php artisan route:clear

清除路由缓存

最后,您可以使用php artisan route:list列出所有路线和动作绑定