Laravel 8 — New routing implementation

Eloïc
2 min readOct 27, 2020

Recently, I have to migrate some personal projects from Laravel 7.X to the latest Laravel version 8.

Considering that will be a smooth migration with the help of Visual Studio code refactoring system, I was dealing with an unexpected error regarding the routing system, saying that the target class doesn’t exist.

Calling the API will return this error

After spending a long time making some testing in order to get rid of this issue (First, I thought I was missing any resources or namespace in my code), It appears that Laravel developers have decided to slightly modify the routing system.

The syntax of binding the string url to the string Controller is not recommended anymore, and this is because in the Service Routing providers, the $namespace attribute is commented by default

For information, The $namespace is the key to how Laravel interprets your routes .

In order to use the new routing system in your web.php or api.php, it is recommended to import the controller class. I believe this will be the standard code for routing your View URL to the Controller.

use App\Http\Controllers\TestController;  Route::get('/test', [TestController::class, 'GetTestMethod']);

But if you prefer the route prefix of Laravel 7.x (or previous version) style controllers, you can simply remove the comment of the $namespace attribute to the RouteServiceProvider.php (pathapp/Providers/RouteServiceProvider.php)

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

For more information regarding this subject, please refer to the official Laravel documentation, link down below.

https://laravel.com/docs/8.x/upgrade#routing

Hope this will save some hours for other developers out there :)

--

--

Eloïc
0 Followers

Passionate about programming software