Target class [PagesController] does not exist in laravel 8

Viewed 6800

This error keeps showing up on the browser even though I had tried a few solutions to fix it.

This is the code for PagesController.php:

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

class PagesController extends Controller
{
    //
    public function index()
    {
        return view("pages.index");
    }
}

Code for routes/web.php:

<?php

use Illuminate\Support\Facades\Route;
use App\Http\Controllers\PagesController;

/*
|--------------------------------------------------------------------------
| 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("/", [PagesController::class, 'index']);

Code for RouteServiceProvider.php:

<?php

namespace App\Providers;

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

class RouteServiceProvider extends ServiceProvider
{
    /**
     * This namespace is applied to your controller routes.
     *
     * In addition, it is set as the URL generator's root namespace.
     *
     * @var string
     */
    protected $namespace = 'App\Http\Controllers';

    /**
     * The path to the "home" route for your application.
     *
     * @var string
     */
    public const HOME = '/home';

    /**
     * Define your route model bindings, pattern filters, etc.
     *
     * @return void
     */
    public function boot()
    {
        //

        parent::boot();
    }

    /**
     * Define the routes for the application.
     *
     * @return void
     */
    public function map()
    {
        $this->mapApiRoutes();

        $this->mapWebRoutes();

        //
    }

    /**
     * Define the "web" routes for the application.
     *
     * These routes all receive session state, CSRF protection, etc.
     *
     * @return void
     */
    protected function mapWebRoutes()
    {
        Route::middleware('web')
             ->namespace($this->namespace)
             ->group(base_path('routes/web.php'));
    }

    /**
     * Define the "API" routes for the application.
     *
     * These routes are typically stateless.
     *
     * @return void
     */
    protected function mapApiRoutes()
    {
        Route::prefix('api')
             ->middleware('api')
             ->namespace($this->namespace)
             ->group(base_path('routes/api.php'));
    }
}

Everything just seems working fine to me, I did add the use statement in my web.php but it still shows PagesController and the default namespace in my RouteServiceProvider.php is 'App\Http\Controllers'. The file structure is also correct the PagesController.php is under the Controllers folder. I also tried to use commands to fix it like php artisan config:cache and composer dump-autoload but it did not work. Does anyone detect the mistake that I am making here? Thanks in advance.

7 Answers

In the new version of laravel, it works like this Add This code

use App\Http\Controllers\PagesController;

and use the route as

Route::get('/', [PagesController::class,'index']);

First check PagesController is not a resource controller. if PagesController is resource controller so you can add bellow code.

php artisan route:list

<?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');
// });

// And use resource route
Route::resource('/', PagesController::class);

use resource route don't added PagesController controller in top. and if not solve please send message me on https://devnote.in I will help you.

If Everything is in the correct folder, I think there is some problem with you route file. Use this statement and check if your Controller can be find:

Route::get('/', 'PagesController@index');

And if it does not work use this one:

Route::get('/', 'App\Http\Controllers\PagesController@index');

I had a similar issue and this is how I solved it. I included the following code in PagesController:

use App\Http\Controllers\Controller;

Then included the following code in routes (web.php)

use App\Http\Controllers\PagesController;
Route::get('/', [PagesController::class,'index']);

Laravel 8 is not a namespace prefix for your route groups where your routes are loaded.

"In previous releases of Laravel, the RouteServiceProvider contained a $namespace property. This property's value would automatically be prefixed onto controller route definitions and calls to the action helper / URL::action method. In Laravel 8.x, this property is null by default. This means that no automatic namespace prefixing will be done by Laravel."

When the namespace prefix is ​​not used, you must use the fully qualified class name for your controllers when referring to them in your ways.

use App\Http\Controllers\UserController;

Route::get('/users', [UserController::class, 'index']); // or Route::get('/users', 'App\Http\Controllers\UserController@index'); If you prefer the old way: App\Providers\RouteServiceProvider:

public function boot() { ...

Route::prefix('api')
    ->middleware('api')
    ->namespace('App\Http\Controllers') // <---------
    ->group(base_path('routes/api.php'));

... }

Do this for any route groups you want a declared namespace for.

The $namespace property:

Release Notes refer to the $namespace property to be set on your RouteServiceProvider in the Release notes and commented in your RouteServiceProvider this does not have any effect on your routes. It is currently only for adding a namespace prefix for generating URLs to actions. So you can set this variable, but it does not add these namespace prefixes, you need to make sure that you are using this variable when adding namespace in the way groups.

information is now an update guide

Laravel 8.x Docs - Upgrade Guide - Routing

With what the Upgrade Guide is showing the important part is that you are defining a namespace on your routes groups. Setting the $namespace variable by itself only helps in generating URLs to actions.

Again, and I can't stress this enough, the important part is setting the namespace for the route groups, which they just happen to be doing by referencing the member variable $namespace directly in the example.

uncomment: you can uncomment the protected $namespace member variable in the RouteServiceProvider to go back to the old way, as the route groups are setup to use this member variable for the namespace for the groups.

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

The only reason uncommenting that would add the namespace prefix to the Controllers assigned to the routes is because the route groups are setup to use this variable as the namespace:

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

    /**
     * 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());
        });
    }
}
Related