Baking with CakePHP

Bake Command Line in CakePHP

One of the many great things about CakePHP is the ability to use the inbuilt "bake" script that will automatically create all your controller and view files including all the source code. This is simply a great tool that will get your project up and running extremely quickly and will take away the monotony of building basic add/edit/delete actions in PHP. This article will begin by setting up everything you need to get "bake" working under windows and will then bake the blog application that was introduced in the CakePHP manual.

The Command Line Interface

First we need to setup windows so that it knows the location of PHP, although this really isn't necessary it will be easier once it's done.

  • Right click on "My Computer" and select "properties"
  • Select the "Advanced Tab"
  • Click the "Environment Variables" button at the bottom
  • Under the "System Variables" list find the "Path" variable and click "edit"
  • Now add the full path of your Php directory in your server e.g. ";C:\server\php" separated by a comma.
System Variables Screenshot

To check that everything is working we will open the "Bake" script that is included with CakePHP.

  • Open the windows command prompt (Start -> Run -> type "cmd" then click 'ok')
  • Navigate to the "Bake" script location by typing "cd C:\server\htdocs\cakephp\cake\scripts"
  • Run the "Bake" Script by typing "php bake.php"

If everything went well you should be seeing the below screenshot, the reason why we added Php to Windows Environment Variables is so that we don't need to implicitly add the full path name to the Php executable (we can type "php" instead of "c:\server\php\php.exe" each time the script is ran).

Creating the Database and Model

We are going to be using the "Posts" table that was used in the Blog Tutorial in the Manual, so hopefully you should already have this setup within PhpMyAdmin. If not create the table by using the SQL statements found in the tutorial. Next we are going to setup the Model, create a new file in the "models" directory and name it "post.php" and enter the following:

class Post extends AppModel {
	var $name = 'Post';
}

Save the file and you are done, this is a very simple Model and will allow your CakePHP application to find the "posts" table you have created in your database. Because Cake works on conventions if you have everything properly named it will do most of the hard work for you which is very useful. All Model files should have a corresponding table in the database (although this is not always the case) but for the time being a singular model is associated to plural in the database.

Baking the controller

Bake Script

Hopefully you should have your command prompt still open but if not repeat the steps above. It will ask you to bake –app so enter "y" and press enter. The script then asks what you would like to bake, lets start with the controller, type "C" and press enter. The script will look in your database and grab a list of possible controllers it can bake in our case this should be just one item "Posts". Press 1 and then enter, we want the script to create the controller automatically so enter "n" for the first question. We are then asked if we want to include some basic class methods, press "y" and then enter. We're going to skip the admin methods (another article coming soon) so press "n" for the next question, and "n" again for the test suite question.

Bake Script

Your Posts Controller has been created and the source code has been written for you, open up the "posts_controller.php" file in the "controllers" folder and have a look for yourself. The basic functions have been included in the file that correspond to actions that you would want the application to do.

  • index(), will get all the posts and send them to your index view
  • view(), will access a single post
  • add(), programming logic to add a new post to the database
  • edit(), will edit a single post
  • delete(), will remove a post from the database
Posts Controller Screenshot

As you can see the view, edit and delete functions have an "id" variable passed as an argument and this will be used in the address bar e.g. "http://cakephp/posts/view/1". When this address is broken down the "posts/" is the controller that we are using, the "view" is the method inside the "posts_controller.php" and the "1" is the "id" variable than the method will have access too. Be sure to read and re-read the Controllers chapter in the Manual to get your head around it.

Baking the Views

Bake Script Views

Next we are going to bake the views for the Post application, run the bake script again in the command prompt and this time choose to bake a View by entering "v" when asked. Similar to when baking the controller you will be given a list of items, again choose "Posts" and choose "n" to let the script create the views automatically. Skip the views for admin routing and it should create the views and all the code automatically.

Have a look in the "posts" folder inside "views" and you will have a file for each of your controller methods excluding delete (this is because delete does not use a view it just redirects automatically). When you load a page in your browser for instance "http://cakephp/posts/" the browser will look at the Posts Controller and look for an index() method, the programming logic is done here and any data that we want to display will be sent to the view using the "$this->set()" function call, the view file will then be called (index.thtml) and this will be displayed in the user. This is done for all the methods in the controller so if you want to display something you will need to create a method and a view file of the same name. Again the manual is an excellent source of information so please read and re-read the manual especially the first few chapters.

Baked Views

Looking at the Generated Code

The code that has been generated is very good for getting up and running very quickly, obviously the coding isn't complete but the basics are all there. Incidentally if you have ever used the scaffolding feature of CakePHP the code that we have just generated is the code that scaffolding runs on. We now have the opportunity to start editing the code for our needs.

Final Baked Blog Application

If we open our application in our browser "http://cakephp/posts/" you will see the index page and are able to add, edit and delete posts. Not too bad for only coding a single Model file and letting Cake do the hard work by coding it for you. Although this is an extremely simple example, I have used the very same process to build a complex application with multiple Models which are all associated to one another. Once the scaffolding has been built you are then able to delve into the code and start changing it to the way you like. Future articles are going to deal with baking more complex applications and associations between models such as adding comments to posts and using tags to categorise your posts.

Wrapping Up

Using Bake to kick start your application is a very easy and efficient way of beginning any project, once you have hand coded the database tables, models, and any associations between them we can use bake to automatically create the basic template code that you can modify to suit your needs. The basic functions that any data driven website will be created for you and this will give you the time to concentrate on the more important things. If you are just starting out with CakePHP you can see just how powerful it can be so please stick with it. The next article will concentrate on setting up admin access to your application with a central control panel and will expand on the simple authentication tutorial in the manual.

Posted on 25th January 2008
on 25/1/08

comments powered by Disqus