Search code examples
laravellaravel-5laravel-8laravel-routing

How to use old Laravel routing style in Laravel 8


I just installed Laravel 8 and in this version, I have to type my routes like this:

Route::get('/admin/panel', [App\Http\Controllers\Admin\PanelController::class, 'index']);

But I got used to Laravel 5 routes which looked like this:

Route::namespace('Admin')->prefix('admin')->group(function () {
    Route::get('/panel', 'Admin/PanelController@index');
});

So how can I use this Laravel 5 routing inside Laravel 8 version?


Solution

  • You can still use it to some extend (e.g. grouping and prefixing a route) but because you're using a FQCN the namespace is redundant. The major advantage using the "new" routing over of the "old" one is refactoring. When you move or rename your controller with an IDE that supports refactoring like PhpStorm you wouldn't need to manually change the name and group namespace of your controller.

    In Laravel 5 you were able to use this route notation too but it wasn't outlined in the documentation.

    To achieve a similar feeling, import the namespace and use just your class names and get rid of the namespace-group in the definition.

    use App\Http\Controllers\Admin\PanelController;
    
    Route::prefix('admin')->group(function () {
        Route::get('panel', [PanelController::class, 'index']);
    });
    

    If you just cannot live with the new way of defining routes, uncomment the $namespace property in app/Providers/RouteServiceProvider.php and you're back to the old way.