SigalZ's avatar

Target class does not exist error - but class exists

Hello,

I have a new installation of Laravel 10 On Windows 10, using Wamp.

I have this file:

App\Http\Controllers\PagesController.php

The code:

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

class PagesController extends Controller
{
    public function index(): void
    {
        dd('in');
    }
}

In the routes\web.php file I have:

use Illuminate\Support\Facades\Route;
use Illuminate\Support\Facades\Artisan;

use App\Http\Controllers\PagesController;

Route::controller(PagesController::class)->group(function () {
    // home
    Route::get('/', 'index')->name('home');
});

Trying to browse the site in a browser I get the error: Target class [PagesController] does not exist.

I ran: php artisan cache:clear, php artisan config:clear, php artisan route:clear and composer dump-autoload.

Nothing helps.

What else should I check please?

Thank you

0 likes
16 replies
vincent15000's avatar

It should work, so it looks like the classes are not loaded.

Have you changed the folder organization of the project ?

Can you show your composer.json file please ?

Something else ... I read my routes rather like below.

Route::get([PagesController::class, 'index'])->name('home');
Snapey's avatar

what is the complete path to the class file?

1 like
SigalZ's avatar

Hi all, I'm sorry for the late reply, I was away.

I think I should mention that I am copying controller, models and views from an old laravel 5.7 project, and trying to make it work going error by error.

This one I can't solve.

@JussiMannisto the error appears on the browser when I try to browse the page.

@Snapey the complete path to the class is: c:\wamp64\www\highlandnew.local/app/http/controllers

@vincent15000 I call the route like I do becuase I'm going to add more calls under the pages controller.

The composer.json file:

Thank you for taking the time to try and help.

1 like
martinbean's avatar

the complete path to the class is: c:\wamp64\www\highlandnew.local/app/http/controllers

@SigalZ Is this actually your path? Because files and folders are case-sensitive. If your folders really are named “http” and “controllers” (all-lowercase), then Composer is not going to find the class when the namespace uses capitalisation (Http, Controllers, etc).

Whenever I see “cannot find file” and “Windows” together, the answer usually boils down to a capitalisation error like this.

SigalZ's avatar

An update, I change the route and added 'page', now http://highlandnew.local/page works.

Route::controller(PagesController::class)->group(function () {
    // home
    Route::get('/page', 'index')->name('home');
});

Why is that?

1 like
SigalZ's avatar

@vincent15000

ReflectionException

Class "PagesController" does not exist

at vendor\laravel\framework\src\Illuminate\Foundation\Console\RouteListCommand.php:225 221▕ if ($this->isFrameworkController($route)) { 222▕ return false; 223▕ } 224▕ ➜ 225▕ $path = (new ReflectionClass($route->getControllerClass())) 226▕ ->getFileName(); 227▕ } else { 228▕ return false; 229▕ }

1 vendor\laravel\framework\src\Illuminate\Foundation\Console\RouteListCommand.php:225 ReflectionClass::__construct("PagesController")

2 vendor\laravel\framework\src\Illuminate\Foundation\Console\RouteListCommand.php:147 Illuminate\Foundation\Console\RouteListCommand::isVendorRoute(Object(Illuminate\Routing\Route))

Snapey's avatar

the base route / can't be used in this way. I'm not sure of the exact reason, but its a restriction just on this route.

Why not use 'home' for the homepage?

    Route::get('/home', 'index')->name('home');
SigalZ's avatar

@Snapey This is strange, because this route works fine on another laravel 10 project I have on my laptop. I will change it to home, but I'm still curious why it doesn't work. Cheers

SigalZ's avatar

Thanks, but this is just the way I wrote it here, of course it has the right caplocks

In the controller:

namespace App\Http\Controllers;
SigalZ's avatar

@ghabe this is my path: c:\wamp64\www\highlandnew.local/app/Http/Controllers thank you

JussiMannisto's avatar
Level 50

You said you're migrating an old Laravel 5 app. Are you also migrating routes, and if you are, do any of them use the old syntax, i.e. PagesController@show?

1 like
SigalZ's avatar

I checked everything again, I thought I emptied the web.php file, but I didn't.

I removed all the code and left only the PagesController code... (Thank you @JussiMannisto) I think this row was causing the problem:

Route::get('info', 'PagesController@info');

Embarrassing... *)

It is working now.

Thank you all very much for all the help.

Please or to participate in this conversation.