Laravel 9 - Target class [PostController] does not exist - Solved

Laravel 9

Nicesnippets

17-05-2022


Laravel 9 - Target class [PostController] does not exist - Solved

Hi Guys

I would like to share with you simple solution of "target class does not exist laravel 9" and "laravel 9 Target class [Controller] does not exist".

Just few days ago launch laravel 9 and i was trying to create my first application with laravel 9 and when i create controller call PostController and when i used with route then i found following issue:

"Target class [PostController] does not exist"

you can see above image too.

Actually this is not an error but laravel 9 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.

Download Laravel


Let us begin the tutorial by installing a new laravel application. if you have already created the project, then skip following step.

composer create-project laravel/laravel example-app

Solution 1: Use on Route File

routes/web.php

<?php

use Illuminate\Support\Facades\Route;

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::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;

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::get('posts', 'PostController@index');

It will help you...