The first thing you will need to do is set up your database and basic application, then get your application to communicate with the database.
Create your database, setting up a username and password to connect to the database
The primary purpose of a CakePHP application is for quick and easy use of the data in a database, so the first step is to create a database with a user and password for access. You can use whatever you want for the username and password, just be sure to keep track of them because you will be adding them to your configuration. With MySQL or MariaDB the commands are:
$ mysql -u root -p'mysql_root_password'
mysql> CREATE USER 'tutorialuser'@'%' IDENTIFIED BY 'userpassword';
mysql> CREATE DATABASE IF NOT EXISTS `tutorialdb`;
mysql> GRANT ALL PRIVILEGES ON `tutorialdb`.* TO 'tutorialuser'@'%';
Install the CakePHP 5 application
Installing CakePHP uses composer typically, so you need to have PHP and composer installed. Installing those is beyond the scope of this tutorial. You can download a zip or tarball if you prefer, but this comes with additional overhead not covered here.
Replace tutorial
with whatever name you want to use for your application.
$ composer create-project --prefer-dist cakephp/app:~5.0 tutorial
This will create the application in a directory of the same name as the application. All future paths will be based on the application root in that directory so I assume you are in that directory after this point.
$ cd tutorial
Install authentication plugin
$ composer require cakephp/authentication
Install the authorization plugin
$ composer require cakephp/authorization
Configure CakePHP to communicate with your database
Edit the file /config/app_local.php
and change Datasources
configuration to match your database settings. If /config/app_local.php
does not exist, copy app_local.example.php
to app_local.php
. With our database settings created above, our database credentials should look like this:
'Datasources' => [
'default' => [
'host' => 'localhost',
'username' => 'tutorialuser',
'password' => 'userpassword',
'database' => 'tutorialdb',
],
],
Be aware that settings in /config/app_local.php
override settings in /config/app.php
and most of our changes will be in /config/app_local.php
.
To verify that your connection is working, browse to your application directory using your web browser (e.g. http://localhost/tutorial ) and you should see a configuration verification page indicating that CakePHP is installed and that the application can talk to your database. If it cannot, check your settings.
Optional Configuration
In addition to the datasource configuration, there are other options in the /config/app_local.php
file. The first one to be aware of is the security "salt." The idea of a cryptographic salt is that the same password, or other value, encrypted in two places, doesn't end up with the same encrypted value. During the installation process CakePHP sets a unique salt for you, though you may want to adjust it to add more randomness.
Another setting is the Session. This isn't listed by default in /config/app_local.php
, but as previously mentioned, settings in /config/app_local.php
override settings in /config/app.php
so just add your change to the bottom of the file.
CakePHP by default uses standard PHP sessions, but you can configure this to use a database for example when you have a load-balanced application across multiple servers. Some handy options for the session are to set your own timeout. I'm unsure what the default is, but it is less than I need to prevent annoying my users. Also a good idea is to set your cookie name unique to other applications so users don't share the same cookie, just in case you have multiple applications on the same server.
Below is an example configuration:
'Session' => [
'defaults' => 'php',
'timeout' => 12 * 60, // in minutes
'cookie' => 'tutorial',
],
Now the basic application is set up and we can start building the data and interface functionality.