Getting started with CakePHP 2.2.5

Hi, currently i am developing a new application using Cakephp. As it is my first development using cakephp framework i fall in some difficulties working with it. I am writing this tutorial in hope that you can configure & run the first page smoothly. I will try to write some more tutorial for intermediate cakephp developer later when i just get enough experience on it. The first thing we need to remember is that Cakephp is a PHP framework that follows the Model-View-Controller standard, which helps developer separate the application layers quite nicely.

Currently MVC convention is becoming a more and more common concept, especially about web applications. We can also point out that Cakephp also follows the Convention-Over-Configuration methodology, which can greatly reduce the amount of code you have to write. Let’s deep to the steps:

Step1: Firstly, create a Database (for example we use tutpub_database). We can do it later, but we did it first so that we can concentrate on the cake.

Step2: Download the cakephp package from cakephp.org. Create a folder (rename it to tutpub_cakephp) in your server directory. Unzip the cakephp package & copy all files & folder to the tutpub_cakephp folder.

Step3: Now its time to change the database configuration information. Go to “/tutpub_cakephp/app/Config/” & rename the “database.php.default” to “database.php“. Open the database.php file and update the “$default” array in “DATABASE_CONFIG” class. For example I have update as follows:

class DATABASE_CONFIG {

    public $default = array(
        'datasource' => 'Database/Mysql',
        'persistent' => false,
        'host' => 'localhost',
        'login' => 'root',
        'password' => '',
        'database' => 'tutpub_database',
        'prefix' => '',
        'encoding' => 'utf8',
    );

    public $test = array(
        'datasource' => 'Database/Mysql',
        'persistent' => false,
        'host' => 'localhost',
        'login' => 'user',
        'password' => 'password',
        'database' => 'test_database_name',
        'prefix' => '',
        'encoding' => 'utf8',
    );
}

Step4: Go to “/tutpub_cakephp/app/Config/” directory and open the “core.php“. update the value of “Security.salt” & “Security.cipherSeed“:

For example:

Configure::write('Security.salt', 'DYhG93b0qyJfIxfs2guVoUubWwvniR2G0FgaC9mi');

to

Configure::write('Security.salt', 'DYhG93b0qyJfIxfs2guVoUubWwvniR2G0FgaC9mi-customtext');

And

Configure::write('Security.cipherSeed', '76859309657453542496749683645');

to

Configure::write('Security.cipherSeed', '76859309657453542496749683645123');

Now you can point out the project folder in your web browser. It is showing that all configuration is in green (nicely done). Now we will create a model, a controller, & a view to view a “hello world” page.

Step5: Go to “/tutpub_cakephp/app/Model/” directory and create a php file with the name “Test.php“. Then put php code below:

<?php
class Test extends AppModel {
    
}
?>

Step6: Go to “/tutpub_cakephp/app/Controller/” directory and create a php file with the name “TestController.php“. Then put php code below:

<?php
class TestController extends AppController {
    
    function index() {
        $data = "Hello World, I have run my first cakephp page!";
        $this->set('data', $data);
    }
}
?>

Step7:  Open the “/tutpub_cakephp/app/View/” directory and create folder “test” & then create a php file with the name “index.php” in it. Then put php code below:

<?php 
echo $data;
?>

Now point out the project to your browser, wish you get a message “Hello World, I have run my first cakephp page!“. Enjoy!

Author Info

Shah Alom

Hi, This is Mohammad Shah Alom, My passion is Programming & Web Development. I am Founder of Micro Solutions Bangladesh. My Facebook profile shahalom1983 & Twitter Profile shahalom_83