ItSolutionStuff.com

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

By Hardik Savani β€’ November 5, 2023
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

Hardik Savani

I'm a full-stack developer, entrepreneur, and founder of ItSolutionStuff.com. Passionate about PHP, Laravel, JavaScript, and helping developers grow.

πŸ“Ί Subscribe on YouTube

We Are Recommending You

β˜…

Laravel 7 Flash Message Example

Read Now β†’
β˜…

Laravel 8 Ckeditor Image Upload Example

Read Now β†’
β˜…

Laravel Clear Cache of Route, View, Config Command

Read Now β†’
β˜…

Laravel Event Broadcasting with Socket.io and Redis Example

Read Now β†’
β˜…

Ubuntu - "/usr/bin/env: β€˜node’: No Such File or Directory" - Solved

Read Now β†’
β˜…

Laravel - "Syntax Error or Access Violation 1055 in Group By" - Solved

Read Now β†’
β˜…

Laravel - Call to Undefined Method Illuminate\Database\Query\Builder::lists() Solved

Read Now β†’
β˜…

PHP - Opencart This Merchant is not Enabled for Auth/settle - Solved

Read Now β†’
β˜…

Laravel - Gulp Error Cannot Find Module Laravel-elixir - Solved

Read Now β†’
β˜…

Git - fatal: Failed to connect to bitbucket.org port 443: Network is unreachable - Solved

Read Now β†’
β˜…

Laravel - Class "Input" not found - Solved

Read Now β†’