How to Create CakePHP Controller,Model and View by Bake console

CakePHP’s Bake console is easy and powerful tool to kick start your project. You will be able to step up a full working application in a few minutes using it.
In this tutorial, we will show you how to use CakePHP’s Bake console to generate model, controller and view codes by simple console commends.
Please note this tutorial requires basic understanding of CakePHP framework, at least you should be able to write a hello world application on your own.
If you have not yet used CakePHP, please read the offical Cookbook. – See more at:

Set Up Bake console

I have already setup a CakePHP project under :

1
C:\wampp\www\personal-tutorial

First thing we are going to do is to verify if Environment Variables is set properly in your windows machine.

Go to Command Prompt(I hope you know what ‘CMD’ is, do a Google search on ‘CMD windows’ if you have no idea.) in windows and type in “cake bake” from any path:

1
C:\Users\Chauhan>cake bake

If you get following message:

1
2
3
C:\Users\xd>cake bake
'cake' is not recognized as an internal or external command,
operable program or batch file.

You will need to set up the Environment Variables; specifically it is the Path variable. Follow the steps below:

  1. Locate ‘Path‘ Variable under System variables section of Environment Variables.
    If you do not know where this setting is, you can follow this link.
  2. Double click on ‘Path‘ variable and bring it up to edit. Before doing any editing, please backup its original value.
    Path variables are separated by ‘;”.
    What we need to do is to add a path pointing to “C:\wampp\www\personal-tutorial\cake\console\” folder.
    So at the end of existing Path value, we add “;C:\wampp\www\personal-tutorial\cake\console\“. Please pay extra attention to ‘;’ at the beginning of the added string.
  3. Finally click on ‘OK’ to save your setting. And restart Command Prompt (setting will only take effect on new Command Prompt). Type in “cake” to verify if it is working:
1
2
3
4
C:\Users\xd>cake
Welcome to CakePHP v1.3.2 Console
---------------------------------------------------------------
******

Step Up Database table


We will need a database table for CakePHP console to generate the codes. Let us create table in our database:

1
2
3
4
5
CREATE TABLE `posts` (
`id` INT NOT NULL AUTO_INCREMENT PRIMARY KEY ,
`name` INT( 250 ) NOT NULL ,
`content` TEXT NOT NULL
) ENGINE = InnoDB;

Bake model


Now open Command Prompt and CMD to “C:\wampp\www\personal-tutorial\app” as below:

1
C:\> cd wampp\www\personal-tutorial\app

Then enter “cake bake” in the Command Prompt:

1
2
C:\>cd xampp\htdocs\personal-tutorial\app
C:\xampp\htdocs\personal-tutorial\app>cake bake

If you get a message similar to this:

1
2
3
4
5
6
Welcome to CakePHP v1.3.2 Console
---------------------------------------------------------------
App : app
Path: C:\xampp\htdocs\personal-tutorial\app
---------------------------------------------------------------
Your database configuration was not found. Take a moment to create one.

This means it is a fresh copy of CakePHP project you are working on, you are asked to create database configuration file (which is “app/config/database.php”). Just follow the instructions, it will automatically generate a database.php for you.

If you get a message similar to this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
Welcome to CakePHP v1.3.2 Console
------------------------------------------------
App : app
Path: C:\xampp\htdocs\personal-tutorial\app
------------------------------------------------
Interactive Bake Shell
------------------------------------------------
[D]atabase Configuration
[M]odel
[V]iew
[C]ontroller
[P]roject
[F]ixture
[T]est case
[Q]uit
What would you like to Bake? (D/M/V/C/P/F/T/Q)

This means you are ready to go. Just enter “Q” to quit first.

Now go back to Command Prompt and do “cake bake model post” as follow:

1
C:\wampp\www\personal-tutorial\app>cake bake model post

You should get message like similar to this:

1
2
3
4
5
6
C:\wampp\www\personal-tutorial\app>cake bake model post
Welcome to CakePHP v1.3.2 Console
---------------------------------------------------------------
App : app
Path: C:\wampp\www\personal-tutorial\app
---------------------------------------------------------------

Baking model class for Post…

Creating file C:\wampp\www\personal-tutorial\app\models\post.php
Wrote `C:\wampp\www\personal-tutorial\app\models\post.php`

You can download SimpleTest from http://simpletest.org

Baking test fixture for Post…

Creating file C:\wampp\www\personal-tutorial\app\tests\fixtures\post_fixture.php
Wrote `C:\wampp\www\personal-tutorial\app\tests\fixtures\post_fixture.php`
Bake is detecting possible fixtures..

Creating file C:\wampp\www\personal-tutorial\app\tests\cases\models\post.test.php
Wrote `C:\wampp\www\personal-tutorial\app\tests\cases\models\post.test.php`

Bake controller


Baking controller is similar to bake model. From the same Command Prompt above, enter “cake bake controller post scaffold” to bake the controller class. A few notes to take:

1
C:\wampp\www\personal-tutorial\app>cake bake controller post scaffold

This will generate standard controller class which all CRUD methods implemented.

1
C:\wampp\www\personal-tutorial\app>cake bake controller post

This will create a scaffolding controller, which means no functions will be defined in the controller, and view files will not be generated.

1
C:\wampp\www\personal-tutorial\app>cake bake controller post scaffold admin

This will prepend “admin_” to each function. This is perfect if you are using any prefix routes. You can change “admin” to any value you needed.

You should get following message for a successful generation:

1
2
3
4
5
6
7
8
9
10
11
C:\wampp\www\personal-tutorial\app>cake bake controller post
Welcome to CakePHP v1.3.2 Console
---------------------------------------------------------------
App : app
Path: C:\wampp\www\personal-tutorial\app
---------------------------------------------------------------
Creating file C:\wampp\www\personal-tutorial\app\controllers\posts_controller.php
Wrote `C:\wampp\www\personal-tutorial\app\controllers\posts_controller.php`
Bake is detecting possible fixtures..
Creating file C:\wampp\www\personal-tutorial\app\tests\cases\controllers\posts_controller.test.php
Wrote `C:\wampp\www\personal-tutorial\app\tests\cases\controllers\posts_controller.test.php`

Bake view


Bake view files is very straightforward. From the same Command Prompt above, and do

1
C:\wampp\www\personal-tutorial\app>cake bake view post

This will generate corresponding views depends on your controller class. So make sure you follow the order correctly, first bake controller, then view.You should get following message for a successful generation:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
C:\wampp\www\personal-tutorial\app cake bake view post

Welcome to CakePHP v1.3.2 Console
---------------------------------------------------------------
App : app
Path: C:\wamp\www\personal-tutorial\app
---------------------------------------------------------------

Creating file C:\wamp\www\personal-tutorial\app\views\posts\index.ctp
Wrote `C:\wamp\www\personal-tutorial\app\views\posts\index.ctp`

Creating file C:\wamp\www\personal-tutorial\app\views\posts\view.ctp
Wrote `C:\wamp\www\personal-tutorial\app\views\posts\view.ctp`

Creating file C:\wamp\www\personal-tutorial\app\views\posts\add.ctp
Wrote `C:\wamp\www\personal-tutorial\app\views\posts\add.ctp`

Creating file C:\wamp\www\personal-tutorial\app\views\posts\edit.ctp
Wrote `C:\wamp\www\personal-tutorial\app\views\posts\edit.ctp`

Finish


 

Now go and check folders “app/controllers”, “app/models” and “app/views”. You will find “posts_controller.php”, “post.php” and “app/views/posts”. They are all auto generated.

Open browser and head to “http://localhost/personal-tutorial/posts”

Leave a Reply