4
votes

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

InvalidArgumentException

Route [dashboard] not defined.

routes/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
Try to clear route cache with php artisan route:clearAlexey Mezenin
Just do what @AlexeyMezenin said in above comment and every thing will be fineMayank Pandeyz

3 Answers

1
votes

as like name(). You should use one of the two :

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

Or

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

After, you clear route cache with php artisan route:clear

Final, you can use php artisan route:list to lists all routes and the action bind

0
votes

Try this:

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

0
votes

When you use as it names the route , so you have add two name dashbbaorddashboard because you use as and name

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

This will work