Home » Errors » How to Fix Target Class Does Not Exist in Laravel

How to Fix Target Class Does Not Exist in Laravel

This exception occurs when the Laravel service container can’t find a class you want to get through dependency injection or directly from the container. Moreover, this does not necessarily have to be the class name. It can also be a class alias.

You can face the error in a few different cases. In this article, I will explain why the error with the message “Target class does not exist” happens in Laravel and how to fix it.


Table of Contents

Fixing Target Class Does Not Exist in Laravel

In most online tutorials, you will see this error related to defining routes. However, as I said before, it is a container error, which means the container does not know how to create the entity you want. The exception with the message is thrown in the Illuminate\Container\Container class:

Let’s look at how to fix the error if it occurs in the routes definition and then look at the container error in detail.

1. Controller Class Without Namespace

In Laravel 7.x and earlier, it was enough to specify only the controller class name and action method during route definition if the controller is located in the app/Http/Controllers folder. In Laravel 8, that feature was removed, and now you have to provide a full path to the controller class. If you use the old way, the service container does not know where to find your controller and throws the error:

routes/web.phpRoute::get('/foo', 'FooController@index');

Now, the code should be like this:

routes/web.phpRoute::get('/foo', '\App\Http\Controllers\FooController@index');

This is one of many ways to solve the issue. I like providing a full controller namespace using the *::class instruction. For ExampleController it will look like this:

routes/web.phpRoute::get('/foo', [\App\Http\Controllers\FooController::class, 'index']);

This notation allows you to jump into the controller using the Ctrl+Click hotkey in your IDE and using IDE refactoring tools to rename or move the controller class.

However, if you prefer the old way, you can add default namespaces for controllers using the namespace() method in boot() method of the RoutesServiceProvider for each route group:

app/Providers/RouteServiceProvider.php$this->routes(function () { Route::middleware("api") ->prefix("api") ->namespace("\App\Http\Controllers") ->group(base_path("routes/api.php")); Route::middleware("web") ->namespace("\App\Http\Controllers") ->group(base_path("routes/web.php")); });

routes/web.phpRoute::get('/foo', 'FooController@index');

2. Class Alias is Not Registered

You also can face the error when you try to get class from the Laravel service container using app()->make(), app()->get() or dependency injection. Service container allows the creation of singleton services that have only one instance for application or bind interface with its implementation. But you do not have to specify the class name as a key. It also can be an alias. You can read more about it in the official documentation.

If you provide a class name with a namespace, the container will just create a new object in case this class is not registered or registered not properly. So that you won’t be notified about this error. However, if you try to get the class by its alias, you will get the target class does not exist error. For example:

routes/web.phpRoute::get('/service', function(){ $service = app()->make('some.service'); });

Firstly, make sure that the alias is properly registered in the boot() method of a service provider and that the service provider is loaded. Usually, all services for the application are registered in AppServiceProvider. However, if you don’t know where is registered your alias, use the ack command in the project folder or IDE searching tools:

sudo ack "some.service"

Ensure that the alias name in the provider and your code are the same:

app/Providers/AppServiceProvider.php$this->app->bind('some.service', \App\Services\SomeService::class);

Then, make sure that the provider is registered in the providers property in config/app.php:

3. Issue in Third-party Package

If you meet this error in a third-party package, there is a small possibility that it is a bug. But it’s not necessary. Firstly, ensure that the package’s service provider is registered and loaded. In Laravel 6 and higher package, developers can add extra -> Laravel -> providers section with a needed provider in composer.json file. Laravel will discover the provider and load it without adding it to config/app.php. For example, laravel/telescope:

This feature can be cancelled in main composer.json of your project in section extra -> laravel -> dont-discover. If the provider is placed in the package composer.json and not placed in the dont-discover section.

Another reason why the required service does not register despite the provider being loaded may be that you are using the package in a way that its developers do not intend. Find and check the documentation of the required package to ensure you use it properly.

Wrapping Up

In this article, I have explained how to fix an error “Target class does not exist” in Laravel. As you can see, there are several reasons why this problem can occur. Still, since this is a global container problem, you should start looking for the cause by checking the correctness of the class name or alias and class registration in the container.

Leave a Comment

Exit mobile version