DEV Community

Cover image for Laravel 8: Routing Change

Laravel 8: Routing Change

Odunayo Ogungbure
Backend developer passionate about building and fixing stuffs, and good music.
ใƒป2 min read

With the release of Laravel 8, there has been a change to the routing syntax which might catch you by surprise, at least it did to me. It wasn't under the high or low impact changes so I skipped reading to the end and it came back to haunt me.

In a fresh laravel 8 install, I have this in my web.php file:

Route::get('/', 'HomeController@index');
Enter fullscreen mode Exit fullscreen mode

and this in the index method of the HomeController.php

     * Display a listing of the resource.
     * @return \Illuminate\Http\Response
    public function index()
        return view('welcome');
Enter fullscreen mode Exit fullscreen mode

This should be very familiar and we'd be expecting a view when we visit the / route. Rather what we see is an error page

FireShot Capture 058 - ๐Ÿงจ Target class [HomeController] does not exist. - localhost

You might start thinking you messed thing up, but no you didn't. Rather, in laravel 8 this syntax doesn't work by default. In the upgrade guide, it was mentioned but the likelihood of impact was optional ๐Ÿคจ. Though this was mentioned:

In most cases, this won't impact applications that are being upgraded because your RouteServiceProvider will still contain the $namespace property with its previous value. However, if you upgrade your application by creating a brand new Laravel project, you may encounter this as a breaking change.

The way to define your routes in laravel 8 is either

use App\Http\Controllers\HomeController;

// Using PHP callable syntax...
Route::get('/', [HomeController::class, 'index']);
Enter fullscreen mode Exit fullscreen mode


// Using string syntax...
Route::get('/', 'App\Http\Controllers\HomeController@index');
Enter fullscreen mode Exit fullscreen mode

A resource route becomes

Route::resource('/', HomeController::class);
Enter fullscreen mode Exit fullscreen mode

This means that in laravel 8, there is no automatic controller declaration prefixing by default.

If you want to stick to the old way, then you need to add a namespace property in the RouteServiceProvider.php (This property was present in previous versions) and activate in the routes method.

     * This namespace is applied to your controller routes.
     * In addition, it is set as the URL generator's root namespace.
     * @var string
    protected $namespace = 'App\Http\Controllers';

    // Edit boot method  
     * Define your route model bindings, pattern filters, etc.
     * @return void
    public function boot()

        $this->routes(function () {

Enter fullscreen mode Exit fullscreen mode

To recap, in laravel 8 you can use the new syntax to define your routes or stick to the old way.

Discussion (4)

wing profile image

I recently came across this problem. Do you know why they made the change? I can't really find anywhere on the internet that mentions the benefits

blessdarah profile image
Bless Darah Gah

The change was made for the purpose of intellisense.

benjaminv profile image

but of what? It is well intellisensed before in both VSC and PhpStorm world.

mr_steelze profile image
Odunayo Ogungbure Author

I can't really say ๐Ÿคท๐Ÿพโ€โ™‚๏ธ