Laravel 8 Auto-Prefixed Controller Routing: Target class does not exist

Laravel 8 Auto-Prefixed Controller Routing: Target class does not exist

In this post we will give you information about Laravel 8 Auto-Prefixed Controller Routing: Target class does not exist. Hear we will give you detail about Laravel 8 Auto-Prefixed Controller Routing: Target class does not existAnd how to use it also give you demo for it if it is necessary.

Laravel 8 officially released on 8th September 2020. The laravel team releases new Laravel version in every 6-month interval with major changes. As Laravel 8 Non-LTS (general version), the Laravel 8 will provide 6 months bug fixes until March 8, 2021, and 1-year security fixes until 8 September 2021.

Among the new changes was the removal of the default route namespacing.

If you create a new Laravel 8 project,there is no namespace prefix being applied to your route groups that your routes are loaded into. This causes en error that says Target class does not exist.

According to Laravel 8.x Docs

“In previous releases of Laravel, the RouteServiceProvider contained a $namespace property. This property’s value would automatically be prefixed onto controller route definitions and calls to the action helper / URL::action method. In Laravel 8.x, this property is null by default. This means that no automatic namespace prefixing will be done by Laravel.”

This simply changes how you reference controllers in your routing configuration file(s).

You’ll need to use the fully qualified class name for your controllers when referring to them in your routes. For example:

use AppHttpControllersHomeController;Route::get('/home', 'AppHttpControllersHomeController@index');

You can also use the following action syntax:

use AppHttpControllersHomeController;Route::get('/home', [HomeController::class, 'index']);

Check out Upgrade Guide – Routing for more information.

If you prefer to add the namespace prefix like previous Laravel versions, simply update the following variable to the app > Providers > RouteServiceProvider.php file:

protected $namespace = null;  

With:

protected $namespace = 'AppHttpControllers';

Next,update the boot() method by adding ->namespace($this->namespace) as follows:

public function boot(){       $this->configureRateLimiting();       $this->routes(function () {            Route::middleware('web')                ->namespace($this->namespace)                ->group(base_path('routes/web.php'));            Route::prefix('api')                ->middleware('api')                ->namespace($this->namespace)                ->group(base_path('routes/api.php'));        });}

As a summary, you have three ways to fix Laravel 8 not able to find your controller with a Target class does not exist error:

  • Add the namespace to the app > Providers > RouteServiceProvider.php file just like previous Laravel versions,
  • Use the fully qualified class name of your controller in your route files when using the string-syntax,
  • Use the action syntax.

Hope this code and post will helped you for implement Laravel 8 Auto-Prefixed Controller Routing: Target class does not exist. if you need any help or any feedback give it in comment section or you have good idea about this post you can give it comment section. Your comment will help us for help you more and improve us. we will give you this type of more interesting post in featured also so, For more interesting post and code Keep reading our blogs

For More Info See :: laravel And github

We're accepting well-written guest posts and this is a great opportunity to collaborate : Contact US