Create Production Ready CRUD Application In 2 Minutes Using CakePHP 3.6

Posted by & filed under CakePHP, MYSQL, PHP, SQL.

Create Production Ready CRUD Application In 2 Minutes Using CakePHP 3.6
In this tutorial we are going to see how to create Production ready application in Just 2 Minutes using CakePHP 3.6. We are going create entire Blog Application in 2 Minutes Using CakePHP Bake Code generation command line tool.

If you have designed your MySQL database design properly with all Foreign key constraint. Then it’s very to generate code for whole application in 2 minutes using CakePHP Bake Command line tool. In this tutorial I am going to use following database design

how-to-seed-mysql-db-design-php

Prerequisites

  1. Composer (Dependency Manager for PHP)
  2. Basic Knowledge Of CakePHP
  3. Knowledge in MySQL
  4. Command Line

Create MySQL Database and Fill/Seed Dummy Data Using PHP DB Seeder Plugin:

Please create new MySQL Database “cake_cms” in your MySQL server. Then please follow the below tutorial, where I have explained how to create tables and populate dummy data in those tables using PHP DB seeder plugin.

How to seed/populate MySQL database with dummy/test data Using PHP DB Seeder

Note: This tutorial has SQL query to create tables

Installation Instructions:

  1. Download and extract CakePHP application.
  2. Open your terminal and go the extracted CakePHP application, Just run the following command to install necessary PHP packages.

     composer install 

Install CakePHP 3.6 Using Composer

All modern PHP framework uses Composer to install dependent packages. So please install Composer using below tutorial in your respective Operating system.

Once you have installed Composer successfully, then open your Terminal (Command line tool) and run following Composer command from your server root folder.

composer create-project --prefer-dist cakephp/app YOUR-PROJECT-NAME

— Note: In the above command replace YOUR-PROJECT-NAME with your project name.

This will download all necessary PHP packages and create CakePHP application in your server root folder (www/htdocs).

Create .env file from .env.default file in config folder:

Next copy `config/.env.default to `config/.env` and keep all your application configurations.


export APP_NAME="Cake CMS in 2 Minutes"
export DEBUG="true"
export APP_ENCODING="UTF-8"
export APP_DEFAULT_LOCALE="en_US"
export APP_DEFAULT_TIMEZONE="UTC"
export SECURITY_SALT="761b5ae3729c9ee45fb6ccc02d8fe61b2a6e9981c8499aac2384a424ec45d5cb"


#DATABASE
export DATABASE_NAME = "cake_cms"
export DATABASE_USER = "root"
export DATABASE_PASSWORD = "mysql"
export DATABASE_HOST = "localhost"

Load .env file Using bootstrap.php:

Next open your bootstrap.php file in config folder, and uncomment following lines (line no: 52) of code to load environment configuration (.env file) to your applications.


/**
 * Uncomment block of code below if you want to use `.env` file during development.
 * You should copy `config/.env.default to `config/.env` and set/modify the
 * variables as required.
 */
if (!env('APP_NAME') && file_exists(CONFIG . '.env')) {
    $dotenv = new \josegonzalez\Dotenv\Loader([CONFIG . '.env']);
    $dotenv->parse()
        ->putenv()
        ->toEnv()
        ->toServer();
}

Connect MySQL Database Using Configuration loaded from .env config file:

Now you can get the configuration your defined in .env file in your application using env() function. Where env(‘CONFIG_NAME’, ‘default_value’) accepts two parameter

– One is config variable name you defined in the .env file
– other is default value if config variable is not defined in that name.

Now I am changing MySQL connection configuration app.php file in the config folder to use configuration defined in .env file.


'Datasources' => [
        'default' => [
            'className' => 'Cake\Database\Connection',
            'driver' => 'Cake\Database\Driver\Mysql',
            'persistent' => false,
            'host' => env('DATABASE_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' => env('DATABASE_USER', 'localhost'),
            'password' => env('DATABASE_PASSWORD', ''),
            'database' => env('DATABASE_NAME', 'cake_cms'),
            'encoding' => 'utf8',
            '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),
        ],

        /**
         * The test connection is used during the test suite.
         */
        'test' => [
            'className' => 'Cake\Database\Connection',
            'driver' => 'Cake\Database\Driver\Mysql',
            'persistent' => false,
            'host' => 'localhost',
            //'port' => 'non_standard_port_number',
            'username' => 'my_app',
            'password' => 'secret',
            'database' => 'test_myapp',
            'encoding' => 'utf8',
            'timezone' => 'UTC',
            'cacheMetadata' => true,
            'quoteIdentifiers' => false,
            'log' => false,
            //'init' => ['SET GLOBAL innodb_stats_on_metadata = 0'],
            'url' => env('DATABASE_TEST_URL', null),
        ],
    ],

Code Generation with Bake Command Line Tool:

Now open command line and go to your project bin folder, and run the following commands to generate scripts automatically using CakePHP Bake Command line tool.

cd C:\XAMPP\htdocs\cake-cms\bin

Next run the following commands to generate scripts automatically.

cake bake all articles
cake bake all articles_tags
cake bake all tags
cake bake all users

In MacOS you run this command like this.

./cake bake all articles
./cake bake all articles_tags
./cake bake all tags
./cake bake all users

Download Premium Only Scripts & 80+ Demo scripts Instantly at just 1.95 USD per month + 10% discount to all Exclusive Scripts

If you want any of my script need to be customized according to your business requirement,

Please feel free to contact me [at] muni2explore[at]gmail.com

Note: But it will be charged based on your customization requirement

Get Updates, Scripts & Other Useful Resources to your Email

Join 10,000+ Happy Subscribers on feedburner. Click to Subscribe (We don't send spam)
Every Email Subsciber could have access to download 100+ demo scripts & all future scripts.

%d bloggers like this:

Get Instant Script Download Access!