Target Class Does Not Exist In Laravel 8

Websolutionstuff | Sep-23-2020 | Categories : Laravel

In this article, we will see target class does not exist in laravel 8 and how to fix target class not found in laravel 8. As you all know laravel 8 has already been released and you can see there are many changes and update in the laravel 8. Many laravel users are facing issues in their new laravel 8 version when they try to load their routes in web.php and they run into an Exception that says something like "Target class [postController] does not exist".

So, let's controller does not exist in laravel 8, laravel 8 target class does not exist and how to solve/fix target class does not exist in laravel 8.

 

target_class_not_found

 

Up to Laravel 7, the RouteServiceProvider.php file had the below code:

here, the namespace variable has stored 'App\Http\Controllers' and declared in middleware and prefix route as below.

protected $namespace = 'App\Http\Controllers';

protected function mapWebRoutes()
{
    Route::middleware('web')
        ->namespace($this->namespace)
        ->group(base_path('routes/web.php'));
}

protected function mapApiRoutes()
{
    Route::prefix('api')
        ->middleware('api')
        ->namespace($this->namespace)
        ->group(base_path('routes/api.php'));
}

 

 

But, In laravel 8 the $namespace variable was removed and the Route declaration changed as below:

 protected $namespace = null;

 public function boot()
    {
        $this->configureRateLimiting();

        $this->routes(function () {
            Route::middleware('web')
                ->group(base_path('routes/web.php'));

            Route::prefix('api')
                ->middleware('api')
                ->group(base_path('routes/api.php'));
        });
    }

 

So, Here are two different solutions for the target class does not exist.

1. Add namespace manually

In this process, you need to add value/path in the $namespace variable and you need to declare in route as well like below.

protected $namespace = 'App\Http\Controllers';
   
    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'));
        });
    }

Now, run again your app all codes are working fine without the "Target Class Does Not Exist" error.

 

 

2. Using Full Namespace in your Routes

 In this solution, you can use full namespace or change all your route declarations like the below code.

Route::resource('posts','App\Http\Controllers\PostController');

 


You might also like:

Recommended Post
Featured Post
How To Integrate Email Template Builder In Laravel
How To Integrate Email Templat...

In this article, we will see how to integrate an email template builder in laravel. Also, you can integrate an emai...

Read More

Feb-22-2023

How To Convert Laravel Query To SQL Query
How To Convert Laravel Query T...

In this article, we will see how to convert a laravel query to an SQL query. Many times we required laravel query builde...

Read More

Oct-27-2022

Laravel 8 Socialite Login With GitHub Account
Laravel 8 Socialite Login With...

In this tutorial we will see laravel 8 socialite login with github account. explains how to integrate OAuth github...

Read More

Oct-25-2021

Laravel 8 Socialite Login with Google Account
Laravel 8 Socialite Login with...

In this article, we will see laravel 8 socialite login with a google account. This post gives you an example of a larave...

Read More

Dec-01-2020