Laravel 8 - Target class [ProductController] does not exist - Solved

By Hardik Savani | September 9, 2020 | Category : Laravel


I will give you simple solution of "target class does not exist laravel 8" and "laravel 8 Target class [Controller] does not exist".

Just yesterday launch laravel 8 and i was trying to create my first application with laravel 8 and when i create controller call ProductController and when i used with route then i found following issue:

"Target class [ProductController] does not exist"

you can see bellow screenshot too.

Actually this is not an error but laravel 8 removed default namespace form RouteServiceProvider.php file. but i will say this good feature if you want to call your controller class from different namespace.

but now if you want to looking for solution then i will give you two solution. you can use in route file or you can define default namespace on RouteServiceProvider.php file. let's see both solution one by one.

I hope it can help you.

Solution 1: Use on Route File

routes/web.php

<?php

use Illuminate\Support\Facades\Route;

use App\Http\Controllers\ProductController;

use App\Http\Controllers\PostController;

/*

|--------------------------------------------------------------------------

| Web Routes

|--------------------------------------------------------------------------

|

| Here is where you can register web routes for your application. These

| routes are loaded by the RouteServiceProvider within a group which

| contains the "web" middleware group. Now create something great!

|

*/

Route::get('/', function () {

return view('welcome');

});

Route::resource('products', ProductController::class);

Route::get('posts', [PostController::class, 'index']);

Solution 2: Define in RouteServiceProvider

second solution is you can define as laravel old version. so let's define it as bellow:

app/Http/Providers/RouteServiceProvider.php

<?php

namespace App\Providers;

use Illuminate\Cache\RateLimiting\Limit;

use Illuminate\Foundation\Support\Providers\RouteServiceProvider as ServiceProvider;

use Illuminate\Http\Request;

use Illuminate\Support\Facades\RateLimiter;

use Illuminate\Support\Facades\Route;

class RouteServiceProvider extends ServiceProvider

{

/**

* The path to the "home" route for your application.

*

* This is used by Laravel authentication to redirect users after login.

*

* @var string

*/

public const HOME = '/home';

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

/**

* Define your route model bindings, pattern filters, etc.

*

* @return void

*/

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')

->group(base_path('routes/api.php'));

});

}

....

routes/web.php

<?php

use Illuminate\Support\Facades\Route;

/*

|--------------------------------------------------------------------------

| Web Routes

|--------------------------------------------------------------------------

|

| Here is where you can register web routes for your application. These

| routes are loaded by the RouteServiceProvider within a group which

| contains the "web" middleware group. Now create something great!

|

*/

Route::get('/', function () {

return view('welcome');

});

Route::resource('products', 'ProductController');

Route::get('posts', 'PostController@index');

I hope it can help you...




Hardik Savani
My name is Hardik Savani. I'm a full-stack developer, entrepreneur and owner of Aatman Infotech. I live in India and I love to write tutorials and tips that can help to other artisan. I am a big fan of PHP, Javascript, JQuery, Laravel, Codeigniter, VueJS, AngularJS and Bootstrap from the early stage.
Follow Me: Github Twitter
***Do you want me hire for your Project Work? Then Contact US.

We are Recommending you: