Create a small CakePHP Application using CakeBake
CakePHP comes with a Bake console which can create CakePHP’s basic structure using database within few minutes of time. It’s not just the skeleton of classes but it can create fully functional structure of CRUD functionality with in few minutes.
This article will explain you how to bake all things in CakePHP 3.x by using Ubuntu operating system. Bake is a PHP CLI console application which generate code as per our database structure. To run this application on Windows, Linux, or Mac operating system, users must add some Environment variables to their system.
First, make sure that you are having a fresh copy of CakePHP 3.x version installed on your system either by using composer or by mannual installation. But If you don’t know how to install CakePHP 3.x, you can read my previous tutorials:
CakePHP is an excellent PHP framework which provides ease to the developer to create applications easily and quickly. So let’s try to build a small application for “Users Management”. For this you need to have the database structure. Here are the tables in MySQL which you need to create before baking in CakePHP.
Database Name: CakePHP
Tables: users, types, countries
Users Table:
Types Table:
Countries Table:
This time we will bake files for just only 3 tables for the establishment of our relationships. As we are having 3 tables in which “users” table is linked with other 2 tables using “type_id” and “country_id” fields. These 2 fields will be used to create the associations (relationships) in CakePHP. CakePHP strictly follows naming conventions for database tables and for coding part as well so you need to be very careful while naming them. Here the table name must be plural and field must be in singular form.
For example: “users, types, countries” are the table names in plural form and “country_id, type_id” are the field names which are singular.
Let’s start baking –
Step 1. Open up your terminal window to run cake commands and get into the cakephp directory with the command
cd /var/www/html/CakePHP;
Step 2. Type “bin/cake” in terminal and hit enter. This will show you the following output.
bin/cake;
Step 3. Type “bin/cake bake” in terminal and press enter and you will get the options which you can use with “bin/cake bake” command to bake CakePHP.
bin/cake bake;
Step 4. Now you can bake everything in one go or you can bake controllers, models, views, etc one by one also. To bake all together use “bin/cake bake all” otherwise to bake just only one thing in single time use “bin/cake bake controller”, “bin/cake bake model”, “bin/cake bake view”. I am using “bin/cake bake all” to bake everything in one go, this will show you the list of the available models on the basis of your database.
bin/cake bake all;
Step 5. As we are having 3 tables, so we have to use the following commands one by one. This will show you “Bake All Complete”
bin/cake bake all users; bin/cake bake all types; bin/cake bake all countries;
Step 6. Open browser and type “http://localhost/CakePHP/users” to have the output of the operations we have performed till now.
Excellent tutorial!! thank you.
I have written a basic application in PHP a while ago and have been looking for a framework to create it in – this is awesome – thx.
Good stuff – I want to add a file upload for the user + view of user files – so I have created an attachment table with user_id and baked it – created the file upload for add.ctp, but the wiring of the model and controller is still not clear to me – can you point me in the right direction. I am using 3.3.15.
Dear Stof,
Thanks for reading our blog and for the appreciation. We have posted one more article on your special request for file uploading using cake bake command. The link for the article is “http://www.qualitians.com/file-upload-in-cakephp-3-using-cake-bake/” or you can find that easily on our home page as well.
Hope you will like our next blog post on “CakePHP File Upload using Cake Bake” as well.