Laravel 8 - Target class [ProductController] does not exist - Solved
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
I'm a full-stack developer, entrepreneur and owner of ItSolutionstuff.com. 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, Laravel, Angular, Vue, Node, Javascript, JQuery, Codeigniter and Bootstrap from the early stage. I believe in Hardworking and Consistency.
We are Recommending you
- Laravel 7 Flash Message Example
- Laravel 8 Ckeditor Image Upload Example
- Laravel Clear Cache of Route, View, Config Command
- Laravel Event Broadcasting with Socket.io and Redis Example
- Ubuntu - "/usr/bin/env: ‘node’: No Such File or Directory" - Solved
- Laravel - "Syntax Error or Access Violation 1055 in Group By" - Solved
- Laravel - Call to Undefined Method Illuminate\Database\Query\Builder::lists() Solved
- PHP - Opencart This Merchant is not Enabled for Auth/settle - Solved
- Laravel - Gulp Error Cannot Find Module Laravel-elixir - Solved
- Git - fatal: Failed to connect to bitbucket.org port 443: Network is unreachable - Solved
- Laravel - Class "Input" not found - Solved