MultipleVirtual

CakePHP installation and using Cake Bake: An easy way!

In this post, we will see step by step procedure of installing and running CakePHP project in WAMP server!

Installation requirements:

Apache with mod_rewrite enabled
PHP 5.2.8 or greater
MySQL /PostgreSQL/MS SQL Server/SQLite

Steps:

1. CakePHP installation

2. Setup and configure the sample project

3. Use Cake Bake to run CRUD for the project

Steps details:

1. CakePHP installation

– Download latest stable CakePHP version from following link

https://github.com/cakephp/cakephp/tags

– Extract the file and then rename with your project name i.e. bookhub.

– Move the extracted folder into /wamp/www/ folder.

2. Setup and configure the sample project

Create database name “bookhub” for bookhub project and run following SQL to create table “books”.

CREATE TABLE books (
id INT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(50),
description TEXT,
created DATETIME DEFAULT NULL,
modified DATETIME DEFAULT NULL
);

Insert one sample row in books table-

/* Then insert one sample row for testing: */
INSERT INTO books (name,description,created)
VALUES ('The Earth', 'This book contains details description about the Earth.', NOW());

Database configuration:

Go to /app/Config/ folder and find file name database.php.default there. Rename the file name into database.php and then open using your editor.
Change the database config as following and then Save the file-

public $default = array(
'datasource' => 'Database/Mysql',
'persistent' => false,
'host' => 'localhost',
'port' => '',
'login' => 'root', // Depends on your phpMyadmin setup
'password' => '123456', // Depends on your phpMyadmin setup
'database' => 'bookhub',
'schema' => '',
'prefix' => '',
'encoding' => 'utf8' );

Now please browse your sample project as following localhost/bookhub. It will show a summary status of the project installation.

3. Use Cake Bake to run CRUD for the project

Setup Environment Variables:

– Go to “Control Panel\All Control Panel Items\System” and then click on “Advanced system settings”.
– Click on “Environment Variables” button.
– In the System variables box, find the “Path” variable and click on “Edit…”.
– Add a semicolon to the end of the current “Path” variable.
– At end of the Pathe variable (after the semicolon) add following lines ( for WAMP )-
C:\wamp\bin\php\php5.5.12\;   //Depends on PHP version
C:\wamp\www\bookhub\app\Console\;
– Click “OK” to save the changes.

Using Cake Bake: Typing and pressing “Enter” from the keyboard

– Open Windows “Command Prompt” from the start menu
– Type “cake” on command screen and press “Enter” from the keyboard. It will display welcome message i.e. “Welcome to CakePHP Console”.
– Type “cd c:\wamp\www\bookhub\app” and press “Enter”. It will change the directory to bookhub\app.

Cake Bake for Model:

– Type “cake bake” and press “Enter”. It will display details command information.
– To bake Model type “M” and press “Enter”. It will prompt for database selection. Write database name “default” and press “Enter” again. It will display table list in numerical order.
– Enter table number from the list. Here we have only one table “books” so we have to type “1” and press “Enter”.
– Follow the next steps to bake the “Book” model successfully.

Cake Bake for Controller:

– Type “cake bake” and press “Enter”. It will display details command information.
– To bake Controller type “C” and press “Enter”. It will prompt for database selection. Write database name “default” and press “Enter” again. It will display table list in numerical order.
– Enter table number from the list. Here we have only one table “books” so we have to type “1” and press “Enter”.
– Follow the next steps to bake the “Books” controller successfully.

Cake Bake for View:

– Type “cake bake” and press “Enter”. It will display details command information.
– To bake View type “V” and press “Enter”. It will prompt for database selection. Write database name “default” and press “Enter” again. It will display table list in numerical order.
– Enter table number from the list. Here we have only one table “books” so we have to type “1” and press “Enter”.
– Follow the next steps to bake the “Books” view successfully.

You have successfully baked and created CRUD for the books table.
Now please go to the Config folder and open routes.php and edit the default route to use “Books” “Index” view file.

Enter localhost/bookhub/books/add in your browser address bar and it will show the form to insert a new book into the database table. After successful submission the form it will display a list of all books from the database.
You can edit, delete books from the list for the testing purpose.

I hope you can find the above information helpful to start as a beginner with CakePHP.

Happy exploring with CakePHP.