Connect CakePHP app to the MySQL database

posted in: PHP | 0

CakePHP is a popular PHP framework that is used to build web applications. It makes building web applications quicker, simpler and uses less coding. It offers a flexible database access layer.

You can install CakePHP either by downloading their website or by using composer.

composer self-update && composer create-project --prefer-dist cakephp/app cms

After the installation of cakePHP in your folder , you can check if it is working by going to the root folder of your CakePHP and issue the following command.

cd /path/to/our/app
bin/cake server

This will start a webserver on the port 8765,  Open up the http://localhost:8765/ and you’ll see the welcome page. If you see all the green dots except being connect to the database then your cakePHP is installed successfully.

Now we will check how to connect our CakePHP app to the database. Open the /config/app.php file your CakePHP installation and replace the values in DataSources.

'Datasources' => [
        'default' => [
            'className' => 'Cake\Database\Connection',
            'driver' => 'Cake\Database\Driver\Mysql',
            'persistent' => false,
            'host' => 'localhost',
            /*
             * CakePHP will use the default DB port based on the driver selected
             * MySQL on MAMP uses port 8889, MAMP users will want to uncomment
             * the following line and set the port accordingly
             */
            //'port' => 'non_standard_port_number',
            'username' => 'mycakephp',
            'password' => 'password',
            'database' => 'mycakephp',
            /*
             * You do not need to set this flag to use full utf-8 encoding (internal default since CakePHP 3.6).
             */
            //'encoding' => 'utf8mb4',
            'timezone' => 'UTC',
            'flags' => [],
            'cacheMetadata' => true,
            'log' => false,

            /**
             * Set identifier quoting to true if you are using reserved words or
             * special characters in your table or column names. Enabling this
             * setting will result in queries built using the Query Builder having
             * identifiers quoted when creating SQL. It should be noted that this
             * decreases performance because each query needs to be traversed and
             * manipulated before being executed.
             */
            'quoteIdentifiers' => false,

            /**
             * During development, if using MySQL < 5.6, uncommenting the
             * following line could boost the speed at which schema metadata is
             * fetched from the database. It can also be set directly with the
             * mysql configuration directive 'innodb_stats_on_metadata = 0'
             * which is the recommended value in production environments
             */
            //'init' => ['SET GLOBAL innodb_stats_on_metadata = 0'],

            'url' => env('DATABASE_URL', null),
        ],

You will see, we changed the four properties that are related to mysql database connection. Those are host, username, password and database. In the above example  we connected our CakePHP app to mycakephp database using the username ‘mycakephp’ and password ‘password’.

After connecting the database we can move on to create our models, controllers and views in our CakePHP app.

Leave a Reply