SnapShooter Backups Server, Database, Application and Laravel Backups - Get fully protected with SnapShooter

CakePHP Bake console in windows

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 in windows. We will define a goal and show you various aspect of the Bake console during the process, and hopefully you will be able to bake your own application after this tutorial. 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.

Goal

The final goal of this tutorial is to generate a full working CRUD(Create,Read,Update,Delete) grid table using Bake console. I will not go through the steps of setting up CakePHP, as stated earlier, you should have known the basis of CakePHP framework. And the version of CakePHP I am using in this tutorial is 1.3.2.

Set Up Bake console

Suppose you have already setup a CakePHP project under

C:\xampp\htdocs\personal-tutorial

So the directory's structure should be similar to:

C:\xampp\htdocs\personal-tutorial\app
C:\xampp\htdocs\personal-tutorial\cake
C:\xampp\htdocs\personal-tutorial\index.php
C:\xampp\htdocs\personal-tutorial\plugins
C:\xampp\htdocs\personal-tutorial\vendors
C:\xampp\htdocs\personal-tutorial\.htaccess

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:

C:\Users\xd>cake bake

If you get following message:

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. img

  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:\xampp\htdocs\personal-tutorial\cake\console*" folder. So at the end of existing Path value, we add ";C:\xampp\htdocs\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:

   C:\Users\xd>cake
   Welcome to CakePHP v1.3.2 Console
   ---------------------------------------------------------------
   ******

If you see the welcome message as above, it means you have Path variable set.

Step Up Database table

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

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:\xampp\htdocs\personal-tutorial\app" as below:

C:\>cd xampp\htdocs\personal-tutorial\app

Then enter "cake bake" in the Command Prompt:

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

If you get a message similar to this:

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:

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:

C:\xampp\htdocs\personal-tutorial\app>cake bake model post

You should get message like similar to this:

C:\xampp\htdocs\personal-tutorial\app>cake bake model post
Welcome to CakePHP v1.3.2 Console
---------------------------------------------------------------
App : app
Path: C:\xampp\htdocs\personal-tutorial\app
---------------------------------------------------------------
 
Baking model class for Post...
 
Creating file C:\xampp\htdocs\personal-tutorial\app\models\post.php
Wrote `C:\xampp\htdocs\personal-tutorial\app\models\post.php`
 
You can download SimpleTest from http://simpletest.org
 
Baking test fixture for Post...
 
Creating file C:\xampp\htdocs\personal-tutorial\app\tests\fixtures\post_fixture.php
Wrote `C:\xampp\htdocs\personal-tutorial\app\tests\fixtures\post_fixture.php`
Bake is detecting possible fixtures..
 
Creating file C:\xampp\htdocs\personal-tutorial\app\tests\cases\models\post.test.php
Wrote `C:\xampp\htdocs\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:

C:\xampp\htdocs\personal-tutorial\app>cake bake controller post scaffold

This will generate standard controller class which all CRUD methods implemen

C:\xampp\htdocs\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.

C:\xampp\htdocs\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:

C:\xampp\htdocs\personal-tutorial\app>cake bake controller post
Welcome to CakePHP v1.3.2 Console
---------------------------------------------------------------
App : app
Path: C:\xampp\htdocs\personal-tutorial\app
---------------------------------------------------------------
 
Creating file C:\xampp\htdocs\personal-tutorial\app\controllers\posts_controller.php
Wrote `C:\xampp\htdocs\personal-tutorial\app\controllers\posts_controller.php`
 
You can download SimpleTest from http://simpletest.org
Bake is detecting possible fixtures..
 
Creating file C:\xampp\htdocs\personal-tutorial\app\tests\cases\controllers\posts_controller.test.php
Wrote `C:\xampp\htdocs\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

C:\xampp\htdocs\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:

C:\xampp\htdocs\personal-tutorial\app>cake bake view post
 
Welcome to CakePHP v1.3.2 Console
---------------------------------------------------------------
App : app
Path: C:\xampp\htdocs\personal-tutorial\app
---------------------------------------------------------------
 
Creating file C:\xampp\htdocs\personal-tutorial\app\views\posts\index.ctp
Wrote `C:\xampp\htdocs\personal-tutorial\app\views\posts\index.ctp`
 
Creating file C:\xampp\htdocs\personal-tutorial\app\views\posts\view.ctp
Wrote `C:\xampp\htdocs\personal-tutorial\app\views\posts\view.ctp`
 
Creating file C:\xampp\htdocs\personal-tutorial\app\views\posts\add.ctp
Wrote `C:\xampp\htdocs\personal-tutorial\app\views\posts\add.ctp`
 
Creating file C:\xampp\htdocs\personal-tutorial\app\views\posts\edit.ctp
Wrote `C:\xampp\htdocs\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": img

The End

If you like our tutorial, please follow us on Twitter and help spread the word. We need your support to continue.