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

Juan Litvin
5 min readSep 9, 2020

--

Laravel 8 was released in September 2020 with a ton of new features and changes. One of those changes was the removal of the default route namespacing.

Although this change is backward-compatible, meaning that older projects that used Laravel 7.x can easily migrate to Laravel 8.x without having to change anything, new projects created in Laravel 8 (starting Sep 8th, 2020) have to take this into account.

Many developers have been facing an issue in their newly created Laravel 8 apps where they try to load their routes and they run into an Exception that says something like:

Bear in mind that your error might not look exactly like this

Don’t worry, your code isn’t broken. What happened is that the way you are used to doing things, and the way most tutorials describe routing don’t work now. But this can be fixed in only a few minutes.

The change

Up until Laravel 7, the RouteServiceProvider.php file had the following code:

RouteServiceProvider.php in Laravel 7.x

What this does is tell Laravel to load the routes in routes/web.php, using the web middleware and the App\Http\Controllers namespace. This, in turn, means that whenever you declared a route using the string syntax, Laravel would look for that controller in the App\Http\Controllers folder:

In Laravel 8, the $namespace variable was removed and the Route declaration was changed to:

RouteServiceProvider.php in Laravel 8.x

Now, Laravel is looking for routes inside your web.php file, as always. It’s also applying the web middleware, as always. But notice that it’s no longer using the previous namespace.

This means that starting in Laravel 8, when you declare your routes using the string syntax, Laravel isn’t going to look for your controller inside App\Http\Controllers.

How do I fix this?

The problem here is that Laravel has no idea where to look for your controller, so all we have to do is let it know! There are 3 ways you can accomplish this:

Adding the namespace manually

This is fairly simple. Go into your RoutesServiceProvider.php file and you’ll see the following:

All you need to do is add the following three lines to this file and Laravel will go back to using the default namespace as in Laravel 7.x:

What did we just do? We declared the $namespace variable with the default Namespace for our controllers and told laravel to use that for our web and api routes.

If you try to run your app again, everything should be working.

Using the full namespace

This one involves changing all your route declarations, but the idea is simple: prepend your controller names with their namespace. See the following example for our PostsController inside the app/Http/Controllers folder.

If you try again, everything should be running smoothly.

Using the Action Syntax

This is the alternative I personally recommend as I find it more typo-proof and in my experience provides better IDE support as we are explicitly telling the code which class to use. Instead of using our usual string syntax, we can use the action syntax where we specify the class and method to use in an array:

Notice here we are not passing the PostsController within quotes but rather PostsController::class, which internally will return ‘App\Http\Controllers\PostsController’. The second value in the array is the method to call within that controller, meaning: “In PostsController.php call the ‘all’ method.

Again, if you try to run your app again, everything should be up and running.

Closing Remarks

By now, your app should be up and running again. If not, please feel free to ask for help. Everyone in the community is eager to give a hand.

Whether you added the namespace manually, specified the full namespace in your routes, or went with the action syntax, what you just did is tell Laravel in which namespace your controllers actually are, so now it actually knows where to look.

If you liked what you read or want to learn more cool stuff related to Laravel, you can follow me on Twitter, where I post about coding, entrepreneurship, and living a better life.

If you or someone you know is looking to hire a freelance Laravel developer, reach out via Twitter or schedule a quick call so we can connect!

--

--

Juan Litvin
Juan Litvin

Written by Juan Litvin

A young mind wanting to share his views and give a voice to the empowerment of minorities.

Responses (37)