How to fix the error ‘Target class does not exist’ in Laravel 8

Tram Ho

Laravel 8 has just been released with a host of changes as well as functionality. One of the changes is the removal of the default route namespace route.

Although this change is backwards compatible, meaning older projects using Laravel 7.x can easily migrate to Laravel 8.x without changing anything, however new projects. apply Laravel 8 from the beginning to pay attention to this

Many developers face the problem of creating a project laravel 8, they try to load routes and encounter some typical Exception like this.

This problem is not a code error, however 99.9% of current Laravel guidelines are not suitable in this case because most of them rely on default namespace to provide.

Change

Until Laravel 7, RouteServiceProvider.php has the following code

The above code tells Laravel that when loading routes defined in routes/web.php file, it will default to use the web middleware and namespace AppHttpControllers . More specifically, that is, every time you declare a new route in routes/web.php file using the following syntax, Laravel will look for the controllers in the AppHttpControllers folder:

Larvel will use the AppHttpControllersPostController.php

However, when coming to Laravel 8, when you declare routes using the same syntax, Laravel will no longer look for the controller in the AppHttpControllers . The main problem is here

How to fix

Well as above, we already know that with Laravel 8, there will be no main default namespace so Laravel will not know where to find your controller. There are three ways for you to tell Laravel this:

  1. Adding the default namespace is similar to what Laravel 7 was before.
  2. Use the path to the controller including the namespace when declaring a new route.
  3. Use the action (recommended) syntax

Manually add namespaces

This is extremely simple. Just go to the RouteServiceProvider.php file and you will see the following code:

All you have to do is add 3 lines of code as below and Laravel will have an additional default namespace similar to Laravel 7:

What did we just do? We declare the variable $namespace and use it as the default namespace for both web and api routes.

If you run the application again, everything should be fine again.

Declare the route to use the full namespace

Using this method you would have to apply all of your routes, but the idea is simple: prepend your controller name with its own namespace. See the example below and you will understand immediately

Continue to try running my app again.

Use the action syntax

This is a recommended solution as it will be better supported by the IDEs because it can see exactly which class has been used. Instead of using the usual string-declared syntax, we can use this action syntax by specifying classes and methods for use in an array.

When using string syntax

Instead use the action syntax

Note we have to call PostController::class instead of using string 'PostController' so it will return 'AppHttpControllersPostController' . The second value passed into the array is the name of the method we will use in the controller.

Again try running your application again and everything should work fine.

Conclude

With a few ways as above hopefully your project will not experience namespace errors and work well.

References:

https://medium.com/@litvinjuan/how-to-fix-target-class-does-not-exist-in-laravel-8-f9e28b79f8b4

Share the news now

Source : Viblo