Join WhatsApp ChannelJoin Now

Laravel 8 – Target class [PostController] does not exist

This post was last updated on April 9th, 2021 at 06:37 am

I would wish to share with you easy answer of “target class doesn’t exist laravel eight” and “laravel 8 Target class [Controller] doesn’t exist”.

Just few days past launch laravel eight and that i was attempting produce|to make|to form} my initial application with laravel eight and after I create controller decision PostController and after I used with route then i found following issue:

“Target class[PostController] doesn’t exist”

you can see higher than image too.

Actually this can be not miscalculation however laravel eight removed default namespace kind RouteServiceProvider.php file. however i’ll say this smart feature if you wish to decision your controller category from completely different namespace.

but currently if you wish to trying to find answer then i’ll provide you with 2 answer. will|you’ll|you’ll be able to} use in route file otherwise you can outline default namespace on RouteServiceProvider.php file. let’s have a look at each answer one by one.

Laravel 8 - Target class [PostController] does not exist

Answer 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']);

Answer 2: Define in RouteServiceProvider

app/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';

    /**
     * The controller namespace for the application.
     *
     * When present, controller route declarations will automatically be prefixed with this namespace.
     *
     * @var string|null
     */
    // protected $namespace = 'App\\Http\\Controllers';

    /**
     * Define your route model bindings, pattern filters, etc.
     *
     * @return void
     */
    public function boot()
    {
        $this->configureRateLimiting();

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

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

    /**
     * Configure the rate limiters for the application.
     *
     * @return void
     */
    protected function configureRateLimiting()
    {
        RateLimiter::for('api', function (Request $request) {
            return Limit::perMinute(60)->by(optional($request->user())->id ?: $request->ip());
        });
    }
}

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

Recommended Posts