The CakePHP Bake console can create any of CakePHP’s basic ingredients: models, views and controllers. And I am not just talking skeleton classes: Bake can create a fully functional application in just a few minutes.
I am going to explain how to bake all in the new version of CakePHP (1.3.3) on Windows computer as it takes a little more tweaking. Bake is basically a PHP CLI console application which generate code according to our database model. To run this application, Windows users must add some Environment variables to their system.
First, go ahead and download a fresh copy of CakePHP and extract it somewhere inside your www folder. Make sure that it is working and is connected to database. If you don’t know how to do it, read my CakePHP installing and the basics tutorial.
Let me presume that our fresh CakePHP install is under c:\\wamp\\www\\cakephp folder. Right click on My Computer, click Properties and then Advanced. On the bottom part of the screen find Path variable, select it and click Edit. After last path add a semicolon and then path to our CakePHP console folder (c:\\wamp\\www\\cakephp\\cake\\console\\) then another semicolon and enter a path to PHP executable (mine is C:\\wamp\\bin\\php\\php5.3.0\\). Click OK and close all.
Let us create the tables for our application which will be a jobs board:
CREATE TABLE IF NOT EXISTS `categories` ( `id` int(11) NOT NULL AUTO_INCREMENT, `name` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL, PRIMARY KEY (`id`) ) ENGINE=MyISAM ; CREATE TABLE IF NOT EXISTS `jobs` ( `id` int(11) NOT NULL AUTO_INCREMENT, `category_id` int(11) DEFAULT NULL, `title` varchar(250) COLLATE utf8_unicode_ci DEFAULT NULL, `body` text COLLATE utf8_unicode_ci, `company` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL, `job_type` varchar(100) COLLATE utf8_unicode_ci DEFAULT NULL, `created` datetime DEFAULT NULL, PRIMARY KEY (`id`) ) ENGINE=MyISAM ; CREATE TABLE IF NOT EXISTS `users` ( `id` int(11) NOT NULL AUTO_INCREMENT, `username` varchar(255) COLLATE utf8_unicode_ci NOT NULL, `pass` varchar(255) COLLATE utf8_unicode_ci NOT NULL, `name` varchar(255) COLLATE utf8_unicode_ci NOT NULL, `location` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL, `address` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL, `email` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL, `web` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL, PRIMARY KEY (`id`) ) ENGINE=MyISAM;
This is easy setup with 3 tables which will be used in our future application. Now, open your command prompt and go to your application folder (c:\\wamp\\www\\cakephp).
Enter this code in your command prompt:
cake bake all
If everything went ok, you should see something like this:
My path is different, so do not mind that. If you get prompted for a database configuration, like I was above, just press Enter to confirm default configuration. After that bake will prompt us for which table in the database do we want to bake an application. So write 1 for our Categories table and press Enter. You should see something like this:
When you get prompted about Unit test classes, always just answer no. After you write n to Unit test and press Enter, Bake will create the controllers and views and exit.
That’s it, you have got a full working application for your categories table. You don’t believe me? Point your browser to your application and call categories controller (for example http://localhost/cakephp/categories ) and you will see nice full working app.
Bake generated the links for List jobs and New job, but it is not working at this moment as we did not bake files for jobs table yet. I will cover this next time.
I am not satisfied with this as we did not have a chance to supply validation criteria for our Categories model, so let’s do this another way. Run cake bake command in your command prompt. Now, you will get a different user interface with full control over what you are baking.
Just follow the steps in this video :
Now, point your browser to your categories controller. Go ahead and try to add an empty category. Validation is working, but messages need some tweaking. Notice how CakePHP added a Pagination below the table with Categories. Nice. And if you click on column names, data is sorted by that column! How cool is that? Do you remember how much time it took us in two parts tutorial to achieve this?
I will finish for today.
Make sure your read our previous tutorials in CakePHP series:
- Installing and the basics
- Basic principles
- Making a small application in CakePHP
- Data validation, routes and more
Next time we will bake files for Jobs and Users tables, create relationship between tables and more. So, stay tuned.