How to Bake a New Project in CakePhp


I use bake to start any project of mine and therefore doesn’t mean that using it produces a complete app unless you only want a CRUD application.

This tutorial is intended for beginners in cakephp. It is still recommended that you look at the blog tutorial in cakephp manual because it has a more complete introduction for the framework.

Take note that I am using the Cakephp 1.2.0.7692 RC3 in demonstrating this little project because it is the latest release as of this writing.

What to do and what do we need to begin?

To start off, we need a project. I opted a mini-library application which consists of
Books
Authors
Book reviews by users

The mini-library will store different books. Each books can have one or more authors and one or more reviews created by users. So we will have the following tables
authors
books
books_authors
book_reviews
users

And they have the following relationships:
books can have many authors
authors can have many books
books can have many reviews
users can have many reviews
a review is owned by a book

Download the SQL and create your database.

Let’s start baking!

Since our topic is about baking, we will maximize the use of cake CLI. First, setup your environment so that it will accept the cake command. For Ubuntu users, it is already available in synaptic or use apt-get

sudo apt-get install cakephp1.2-scripts

For windows users, I used this tutorial for setting up Windows.
The following steps is done in Windows so I cannot guarantee the result will be the same in Ubuntu.

Once you have setup your environment, everything is just easy. I do not think I even need to tell you what you need to do but I still will since I already started it:P

You just need to remember this steps:
  1. Create a project. This creates all the files and directories under the /app directory
  2. Configure database.php. We will do that using the command line
  3. Start creating your Models. As the blog tutorial mentioned, models are the bread and butter of cakephp.
  4. Create your controllers
  5. Finally, create your views
  6. Check your new app!

So let's start doing something and stop talking eh?

Open your command line and go to your www directory or wherever you want to create your cakephp project. Do not yet create folder for this project. So in my case, my web directory is E:\www\. Type

cake bake 

It will ask you to create your project since it cannot find an existing one.

Step 1: Create project
I created mylibrary project. Here is the output.

CakePhp bake step 1 - Create Project

Step 2: Configure database
Then it will ask you to create your database config. In my example, my database name is cakedemo_1.

CakePhp bake step 2 - Configure database

And we're done with the installation. If you will check your files, mylibrary folder is created in E:\www. mylibrary already contains all the necessary files and folders to start building your app. To check, go to http://localhost/mylibrary.

CakePhp bake step 3 - Installation result

Do we have the same result?

There were 3 things that happened here in the background.
  • In mylibrary/app/config/core.php, the Security.salt was modified
  • mylibrary/app/config/database.php was created
  • In mylibrary/app/webroot/index.php, CAKE_CORE_INCLUDE_PATH was changed to the absolute path of your cake core library, in my case "E:www\minilib" because this is what I entered in my Path settings (remember this tutorial?) So with this, you have a centralized cakephp core library.

There you have it, a new cakephp project! Let's proceed.

Step 3: Create models, controllers and views
Next step is to create our library application. Again, type cake bake in your command line. The Interactive Bake Shell will appear to ask for more directions. You must ofcourse create first your models, then controllers and finally views. Afterwards, you should have the following files like mine:

CakePhp Bake Step 3 - result

If you already have that, congratulations! Your done! ;) Visit the Books page in http://localhost/mylibrary/books/ to start adding your data.

More tricks you can try out
There are also shortcuts if you only want to create models
cake bake model

or controllers
cake bake controller

or views
cake bake view

For the next post, I'll tackle how to set up different authentication components. Till next time. Happy Baking!

Tags: ,

5 Responses to “How to Bake a New Project in CakePhp”

  1. mzee.richo Says:
    Nice stuff . i normally only bake the models . The rest i hard code , bse cake gives me more than i need at times . Any way nice begginer tutorial .
  2. rachel Says:
    @mzee.richo thanks :) . I usually includes the controller and views with NO scaffolding as well because it makes it easier to just edit or delete parts I do not need.
  3. rbahaguejr Says:
    cake bake all –> makes life a lot easy.
  4. Links on how to bake in CakePHP « Myles Kadusale’s Blog Says:
    [...] how to bake in CakePHP For your baking needs refer to the links below Bake Utility Baking MVC Bake a new Project You could also try cake bake [...]
  5. 13 excellent CakePHP tutorials | blogfreakz.com Says:
    [...] How to Bake a New Project in CakePhp [...]

Add Comment

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>