Had a few hours free yesterday, decided to try out CakePHP 4 after using version 3 last about a year ago. Here are a few gotchas I ran into. Easy to solve, but took a bit of Googling to find…
Out with the old XAMPP
First thing, while CakePHP 4 does say it will run on PHP 7.2, but the new Twig plugin that comes with newest versions seems to want a slightly newer version of PHP 7.2 than what my 2-3-year-old install of XAMPP provided. Well, it was due for an update anyway…
So off to https://www.apachefriends.org/ and picked the most current version. I usually install it on something like c:\xampp because on Windows it gives a warning about some kind of warning about not installing it under a user account.
If you have a previous version of XAMPP installed, you might have to update your PHP. Keep in mind that you can have several versions of PHP on your computer, the command line on Windows comes from the environmental variables. So pull up a console. (“cmd” in the Windows search bar will do) and enter: php -v
You should get something like:
PHP 7.4.4 (cli) (built: Mar 17 2020 13:49:19) ( ZTS Visual C++ 2017 x64 ) Copyright (c) The PHP Group Zend Engine v3.4.0, Copyright (c) Zend Technologies
There’s a couple of other options like “php -h” (help), “php -i” (basically everything about the version that’s running, including where it’s looking for files in – handy)
If you don’t see the expected version of PHP, your system might be pointing at another version. Look up the environmental variables. If you type that into windows you should get a suggestion to edit the system environmental variables. Pick that one and you should get a pop-up with the Environmental Variables button near the bottom. Pick that and then look in the System Variables -> Path section. You should see an extry for the where it’s looking for PHP on the computer.
It should be looking in your XAMPP folder, if not, just edit, save/Apply, close the window (or start a new one) and type “php -v” again. Make sure you do this in a fresh console – the new settings aren’t automatically changed in an existing console.
You’re also going to need Composer over at https://getcomposer.org/ I used the Windows installer that lets you set it up globally so that you can enter commands like “composer update” without needing to put “php” before it.
To test, in a new console, enter “composer -V” to get the currently installed version.
Composer is going to be used to install CakePHP.
Set up a VHost
Once that’s running, go into the C:\xampp2\apache\conf\extra\httpd-vhosts.conf folder and added in a virtual host entry like so:
Listen 8081 <VirtualHost *:8081> ServerName jwt-test.com ServerAlias www.example.com example.com DocumentRoot c:/xampp2/htdocs/jwt-test </VirtualHost>
“jwt-test” was just a name I was going to use, doesn’t exist yet. In fact, don’t create this folder, we’re going to do with composer in a few steps.
All this is telling Apache is that when a user goes to http://localhost:8081/ it will look in here for your app.
One thing to do first though. You need to edit the php.ini file (C:\xampp2\php\php.ini) to uncomment ext-intl extension. Look for the line like:
;extension=intl and then remove that “;” (comment) before it and re-save the file. There are some other extensions that CakePHP requires, but in 7.4 on XAMPP, this seems to be only one not already enabled.
Next, restart Apache and MySQL. If everything is running properly, you should see that port number (8081) on the list under Port(s) and both Module buttons are green:
Finally, Cake is usually paired with a SQL database (MySQL, Maria, PostgreSQL). So we’ll set up too.
Enter http://localhost/phpmyadmin/ into your browser and you should get the MySQLAdmin page up. Let’s create a new, empty database so it’s there for Cake. Click the New link on the top of the column of databases. In my case, I entered “jwt-test” as the name and pressed Create.
This will set up a new database with no tables, which is fine to get set-up.
Now for that Cake…
Lets install CakePHP now. In your console, cd into the htdocs folder and run composer like so.
c:\xampp2>cd htdocs c:\xampp2\htdocs>composer create-project --prefer-dist cakephp/app:4.* jwt-test
If you get an error about a non-empty folder, keep in mind Composer needs a fresh folder to write to. if you’re initializing a Git project, let Composer do its thing, then set-up Git. If there’s other errors make sure you’re running the right version of PHP (php -v again). You’ll probably get some warnings, but you can probably ignore them for now.
Update: the developers of CakePHP fix things pretty fast – this issue was fixed in the next release after this post was originally written. I’ve left it in to show to configure datasources.
Try going to 8081, and you should get a welcome screen…or not.
Bacially this saying the debugging plugin can’t connect to the database (it used the SQLLite by default which doesn’t seem to set-up in
Fortunately, there’s a quick fix for it here: (thanks Manojkumarbashnet) https://github.com/cakephp/debug_kit/issues/671#issuecomment-570488731
In the C:\xampp2\htdocs\jwt-test\config\app.php file add a debug_kit data source. In Cake, you can have multiple data sources for different applications.
This is what I used:
'debug_kit' => [ 'className' => 'Cake\Database\Connection', 'driver' => 'Cake\Database\Driver\Mysql', 'persistent' => false, 'host' => 'localhost', //'port' => Dbport, 'username' => 'root', 'password' => '', 'database' => 'jwt-test', 'timezone' => 'UTC', 'flags' => , 'cacheMetadata' => true, 'log' => false,'quoteIdentifiers' => false, 'url' => env('DATABASE_URL', null), ],
Note the MySQL server that comes with XAMPP has a user of root (no password). The name “jwt-test” is the database we set up earlier. You could also create a second database (“debug_kit”?) and use that instead as debug kit will create 2 tables in there for its own use. Obviously not going to use this a production system!
If you’re having problems connecting to the database still, check if you’ve set up the same root/(no password) in either the same app.php file (below the debug_kit settings in the screenshot) or in C:\xampp2\htdocs\jwt-test\config\app_local.php
'Datasources' => [ 'default' => [ 'host' => 'localhost', /* ... */ //'port' => 'non_standard_port_number', 'username' => 'root', 'password' => '',
Add the username/password under the default datasource in either file. I prefer to use the main app.php file as the less files to keep track of, the better 🙂
Lets refresh the page again and…
If all goes well, this is what you should see. You can head over to the tutorial on CakePHP’s website and get started.