I recently started playing with CakePHP to see what it can do. I decided to put my findings in a tutorial. I will use the ‘Interactive Bake Shell’ feature that comes natively with CakePHP.
Why is this helpful? Well by only populating the database structures (tables and fields) you will be able to use bake to setup all the required files related to the MVC (Model-View-Controller) structures to get a simple CRUD (Create, Read, Update, Delete or similarly Add, View, Edit, Remove) in the CakePHP framework.
Before we start
- I’m assuming you have xampp for windows installed and know the location of your ‘php’ and ‘htdocs’ folders. (In my case ‘d:\xampp\php\’ and ‘d:\xampp\htdocs\’).
- You’ve downloaded a copy of CakePHP (At least version 2.0.0 as of the writing of this post)
- I don’t make use of any IDE in this tutorial as there is no need to, but you might want to use your favourite one to modify any of the files afterwards.
- Know how to use and access the command prompt window (basically go to ‘start’ and type/run ‘cmd’).
- I don’t give any background introduction to CakePHP or the MVC Process.
So we are about to start. There are 3 easy steps to get our CRUD interface up and running. First we’ll setup our database with the table and fields we want to be used as objects. Then we’ll copy the CakePHP installion files and then lastly we’ll go through the command prompt process to run the ‘Interactive Bake Shell’ to generate the ‘.php’ and ‘.cpt’ files in the related ‘app\Model\’, ‘app\View\’ and ‘app\Controller\’ folders.
Step 1 – Import Database
In this example I have a database called ‘bake_test’ and the table we are going to create is called ‘books’. You can use the following SQL code to import the database structure.
CREATE TABLE IF NOT EXISTS `books` ( `id` INT(11) NOT NULL AUTO_INCREMENT, `Author` VARCHAR(255) COLLATE utf8_unicode_ci NOT NULL DEFAULT '', `Title` VARCHAR(255) COLLATE utf8_unicode_ci NOT NULL DEFAULT '', `Desc` text COLLATE utf8_unicode_ci, `Date` DATE DEFAULT NULL, PRIMARY KEY (`id`) ) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci AUTO_INCREMENT=0 ;
Step 2 – Setup Files
Create a folder called ‘cakePHP_baketest’ in your ‘xampp/htdocs/’ folder and copy your downlaoded CakePHP files into this directory.
Step 3 – Run bake through command prompt
Now we are going to run bake to generate the Models, Views and Controllers for our web application. I found this blog post and did the following:
Open the command prompt window and do the following:
- Change the directory to your ‘xampp\php’ folder (in my case ‘d:\xampp\php’).
- Run php.exe with the location of your cake console file (in my case I typed the following ‘php.exe d:\xampp\htdocs\cakePHP_baketest\app\Console\cake.php bake’) (Note: ‘d:\xampp\htdocs\cakePHP_baketest’ is where we copied our CakePHP framework earlier).
- If everything is working correctly you will be shown a welcome screen and asked to setup a configuration file to connect to the database.
Follow the prompts and enter the realted database details like username, password, database etc. If it is a default xampp installation you’ll be able to use the same details as above, taking note that the password field is blank (i.e. ”).
After verifying that everything is ok, typing ‘y’ and pressing the enter key you’ll see a message that the file was created successfully and it will display the location of the ‘database.php’ file.
Then you will be presented by an ‘Interactive Bake Shell’ screen. The first option ([D]atabase Configuration) is what we just did so we can continue to the second option to create the models. Press ‘m’ and then the enter key. Seeing as we only have one table in our database we only have one Model option. Press ’1′ and then the enter key.
The bake script is unable to detect the ‘displayField’ so you are prompted to select it manually. In this case I selected ’3′ (Author). You can also add validation criteria to your forms so press ‘y’ and then the enter key to continue.
The script will go through all the fields and then ask you to select what kind of validation you want to do. I’ve just selected ’21′ (Not Empty) for the ‘id’, ‘Author’ and ‘Title’, but you can be adventurous if you want to try something different.
As you go through the different fields also note that you can add multiple validations, but for simplicity I would suggest keeping it to one validation for now as some combinations will require you to edit the php files afterwards (like max/minlength).
You’ll be prompted to confirm that everything looks ok.
You’ll get a confirmation message showing where the Model and related files are generated. Then the ‘Interactive Bake Shell’ menu will appear again and this time we have to create the controllers, so select ‘c’ and then press the enter key. We have to do controllers before views because the views are dependant on the controller files. Maybe the order should of been MCV, but then it might confuse Red Alert players (MCV = Mobile Construction Vehicle)
Follow the prompts as above and there are some other features we not touching on now, like:
- using components
- adding extra helpers
- additional admin controls (for admin views to be defined later)
Once you done with baking the controller a message will appear to show where the files have been generated.
Ok, so 2 down and 1 more to go. We only have to run the script to generate the Views then we are done. Press ‘v’ and then the enter key to start the process.
Follow the prompts as above again to go through the steps. Once again we are taking the short route and skipping things like:
- admin routing
Ok, so now we are done creating the files and we can now press ‘q’ and then the enter key to close the command prompt.
Open your browser and navigate to ‘http://localhost/cakePHP_baketest/books’ and you’ll see a screen similar to the one above. From here you can navigate to differnet screens from adding, editing, viewing and deleting book objects.
Have fun and thanks for reading! Some more screenshots below.[caption id="attachment_36" align="alignnone" width="590" caption="Viewing a book"][/caption]