Search code examples
phplaravellaravel-5laravel-routinglaravel-5.1

Laravel route Action not defined


I am having trouble with Laravel routes. I'm trying to redirect to a controller after some middleware in the routes. But there is always this error.

The error is:

InvalidArgumentException in UrlGenerator.php line 558: Action App\Http\Controllers\DashboardController@index not defined.

The route code is:

Route::get('/dashboard', ['middleware' => 'auth', function() {
    return Redirect::action('DashboardController@index', array('user' => \Auth::user()));
}]);

The controller:

class DashboardController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return Response
     */
    public function index()
    {
        return view('dashboard')->with('user', \Auth::user());
    }
}

But the above code actually works (so I guess that the controller actually works):

Route::get('/testdashboard', [
    'uses' => 'DashboardController@index'
]);

So what is the problem? What is a valid route action?


Solution

  • This is might a better way to do it, change from

    Route::get('/dashboard', ['middleware' => 'auth', function() {
      return Redirect::action('DashboardController@index', 
      array('user' =>      \Auth::user()));
    }]);
    

    to

    Route::get('/', [
      'middleware' => 'auth',
      'uses' => 'DashboardController@index'
    ]);