0
votes

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?

3

3 Answers

1
votes

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'
]);
1
votes

This is rather a comment than a post, but I can't send it at this time. I don't undestand why do you pass parameter (\Auth:user()) to a method that doesn't require it (but it's correct when you do it for the View).

Anyways I suggest you to work on your Middleware

public function handle($request, Closure $next)
{
    if (Auth::check()) {
        return redirect(...);
    } else {
        return redirect(...);
    } 
}
0
votes

Use this route in place of your route and upgrade your Laravel Project to Laravel 8:

Route::middleware(['auth:sanctum', 'verified'])->group(function () {
    Route::get('/dashboard', 'DashboardController@index')->name('daskboard');
});