phplaravelrouteslaravel-11

Laravel 11 Controller Routes


I have upgraded to Laravel 11 and obviously having to update some code. According to the docs, you point to a controller class with use:

use App\Http\Controllers\UserController;

and the route entry using this class

Route::get('/user', [UserController::class, 'index']);

My question is there anyway to include whole directory of controllers like it has been previously. Unless I am mistaken it seems a web.php file which previously would have been 'use' to reference a directory containing all the Controllers:

use App\Http\Controllers;

then

Route::get('/user', 'UserController@index');
Route::get('/', 'Home@start');
Route::get('/route3', 'Route3class@method');
Route::get('/route4', 'Route4class@method');

now in the Laravel 11 way needs to add a 'use' for each class so:

use App\Http\Controllers\UserController;
use App\Http\Controllers\Home;
use App\Http\Controllers\Route3class;
use App\Http\Controllers\Route4class;

then

Route::get('/user', [UserController::class, 'index']);
Route::get('/', [Home::class,'start');
Route::get('/route3', [Route3class::class,'method');
Route::get('/route4', [Route4class::class,'method');

Or is there actually a Laravel 11 way of including all the Controller classes in a directory with a single use statement


Solution

  • You can do :

    use App\Http\Controllers as Controllers;
    

    and then in routes

    Route::get('/user', [Controllers\UserController::class, 'index']);