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:
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'));
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.
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.
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.
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
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?
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?
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.