Class exists but can't be found

I downloaded cakephp version 5.0.6 and added my version 3 code step by step because I couldn’t get the major revision from version 4.5 to version 5 to work. I installed the Authentication plugin using composer and I followed the instructions at
https://book.cakephp.org/5/en/tutorials-and-examples/cms/authentication.html
I added all of the code from the tutorial to src/Application.php but I get a fatal error that I can’t fix:
Fatal error: Uncaught Error: Class “App\Application” not found in C:\xampp\htdocs\ppv5-0-6\webroot\index.php:34
In my src/Application.php I have:
namespace App;
and
class Application extends BaseApplication implements AuthenticationServiceProviderInterface
I tried doing a composer dump-autoload because a similar post said that would correct the problem but it didn’t help. My composer.json and composer.lock files seem to be up-to-date and both use cakephp version 5. composer.json maps the App namespace to the src directory as follows:

    "autoload": {
        "psr-4": {
            "App\\": "src/"
        }
    },

Why is this post tagged for plugins? I don’t see anything plugin related in it.

Your class is in C:\xampp\htdocs\ppv5-0-6\src\Application.php?

It’s a little bit confusing because the authentication tutorial calls it a plugin even though it gets installed in the vendor folder. The first sentence of the tutorial reads: “Now that our CMS has users, we can enable them to login using the cakephp/authentication plugin.” so I thought it was a plugin.

The class is not in src\Application.php. The error is thrown on line 34 of webroot\index.php which is this line:

$server = new Server(new Application(dirname(__DIR__) . '/config'));

No namespace is declared for webroot\index.php and the classes given are

use App\Application;
use Cake\Http\Server;

I created fully qualified class names by deleting the use statements and prepending them to the folder names:

$server = new \Cake\Http\Server(new \App\Application(dirname(__DIR__) . '/config'));

The error persisted.

A plugin is anything that’s not part of your core application. If you wrote it, it could go in the plugins folder. If someone else wrote it, it goes in the vendor folder (put there by composer). So the authentication tutorial is right to call it a plugin, and it’s also correct that it’s in the vendor folder.

Now, you say the class is not in src\Application.php. Where is it? What’s the exact complete path to the file that has that class? C:\xampp\htdocs\ppv5-0-6\src\Application.php is 100% where it needs to be in order to be found by the autoloader. If your directory structure is not exactly right, things will not work, in exactly the way that you are saying it’s not working. Note that I’m talking here about directory structure, not namespace.

$server = new \Cake\Http\Server(new \App\Application(dirname(__DIR__) . '/config'));

and

use App\Application;
use Cake\Http\Server;
$server = new Server(new Application(dirname(__DIR__) . '/config'));

are exactly equivalent. Using one vs the other is no real change, and will certainly not change anything about how the autoloader goes about finding the implementation of your classes.

I wanted to see the path of the file that is supposed to be loaded so I echoed out DIR:

require dirname(__DIR__) . '/vendor/autoload.php';
echo __DIR__;

OUTPUT:
C:\xampp\htdocs\ppv5-0-6\webroot

vendor isn’t in the webroot folder so maybe it’s not accessing the autoload file.
I hardcoded it to find the autoload file:
require dirname('C:/xampp/htdocs/ppv5-0-6/vendor/autoload.php');

and got the error “failed to open stream: Permission denied”
I’m confused about which file dirname(__DIR__) . '/config') finds.

require dirname(__DIR__) . '/vendor/autoload.php';
is in webroot/index.php

__DIR__ is the absolute path to the folder which the current PHP file is located in, e.g.

/Users/kevinpfeifer/Documents/Sunlime/alfred/webroot

dirname(__DIR__) is 1 directory above the one from above, so

/Users/kevinpfeifer/Documents/Sunlime/alfred

You just echoed __DIR__ which of course is just the webroot folder.
Your problem lies within the fact, that you have a permission problem

failed to open stream: Permission denied

I can’t help you with Windows Permissions Problems though since Windows is the devil and I don’t touch that anymore.

That means that the file that is being required is

require dirname('C:/xampp/htdocs/ppv5-0-6/webroot/vendor/autoload.php');

which makes no sense because there is no vendor folder inside of webroot.

No, it is
dirname('C:/xampp/htdocs/ppv5-0-6/webroot') . '/vendor/autoload.php';
which is
'C:/xampp/htdocs/php5-0-6' . '/vendor/autoload.php';
which is
'C:/xampp/htdocs/php5-0-6/vendor/autoload.php';
which I very much hope exists.

Thanks, Zuluru. I didn’t recognize that dirname is a PHP expression.

Also, I hardcoded the directory path incorrectly which is why I got a permission error.

I couldn’t get the migration from cakephp 4.5 to 5 to work so I downloaded cakephp version 5.0.6 and added snippets of my version 3 code step by step.
After some success, I followed the following tutorial to a T:
https://book.cakephp.org/5/en/tutorials-and-examples/cms/authentication.html
However perhaps some arbitrary deprecated code is interfering with the fresh installation and that’s why the php file can’t find the class?
My idea is to start from scratch with a new installation of cakephp 5.0.6 and get the authentication working before I add more old code to the project. What do you think? It’s going to take a long time to start over so I have to decide first if it’s worthwhile.
I don’t know how to change mytag from Plugins to Need Help.

Upgrading from CakePHP 4 to 5 doesn’t change anything how composer does the autoloading of classes.
Besides the fact that nothing major has changed from the app template between CakePHP 4 and CakePHP 5 as you can see here: Comparing 4.x...5.x · cakephp/app · GitHub

See e.g. how I did it in the last cakfest: https://www.youtube.com/live/GaCqzBuX5sc?si=RBWp2tXkQZ_FVE9Y&t=11274

Following this guide: Installation - 5.x
It says “Your webserver’s PHP version must also be of 8.1 or higher, and should be the same version your command line interface (CLI) uses.”
I have php 8.2.16 for my command line interface and I have 8.0.28 for my XAMPP server. Could this be the reason for my problems?

CakePHP 5 requires PHP 8.1 so yes, if your webserver isn’t running at least PHP 8.1 then you will have problems.

I’m going to install XAMPP with PHP 8.2.12 tomorrow. Do I need to have exactly the same version as my command line interface PHP or is my CLI PHP version 8.2.16 close enough?

it doesn’t need to be exactly the same PHP version, but at least the same minor version

Did you mean major version?

Major versions are PHP 5, 7 and 8
Minor versions are PHP 5.6, 7.0, 7.1, 7.2, 7.3, 7.4, 8.0, 8.1, 8.2 and 8.3

See https://semver.org/

OK, thanks for the clarification, KevinPfeifer. Then my new XAMMP with PHP 8.2.12 and my current CLI PHP version 8.2.16 are both 8.2.x so they’re the same minor versions.