How To Use CakePHP Framework, A Getting Started Guide!

Introduction

Using CakePHP Framework is one of my most favorite things to do. This is the first PHP framework I worked with, and until now, I’m still delighted by it. Now it is time for me to share my knowledge in CakePHP web development.

I think you are here because you already know what CakePHP is, but for those who has no idea yet, and don’t want to read more on CakePHP.org, here’s a brief description: CakePHP makes building web applications simpler, faster and require less code.

I think there are really few blog posts out there with regards to beginner CakePHP tutorials. Most of them are also outdated. We will keep this CakePHP posts as updated as possible. So to the future reader of these posts, please drop something in the comment section below if you think there is something outdated!

We will start with the most basic thing to advanced. Hopefully I can finish this tutorial series in the shortest time possible.

Install CakePHP on Your Server

Below are few steps to make CakePHP alive in your hosting server, see sections 2.1 to 2.4 below.

By the way, if you’re a super beginner and using a windows PC (localhost), you can follow this tutorial first to set up your localhost: 3 Steps to Install WAMP on Windows

Download CakePH

So… what do you expect the first step will be? Of course, we will download the framework. Download the latest CakePHP version here: http://cakephp.org/

As of this writing, the version is CakePHP 2.3.6 stable.

Extract the ZIP file.

Put CakePHP on Your Hosting

I don’t know, but I think most of you guys are using a localhost (your PC or something). Okay, so I’ll assume you are all using your localhost. If you’re not, just give a comment so we can try help you with your issue.

After you download and extract the Framework files, you have to put it in your root directory. I’m using windows 8 and running with WAMP server, so in my case, my root directory is in:

C:\wamp\www\

Now after putting the extracted folder, my CakePHP directory is in:

C:\wamp\www\cakephp-cakephp-b81c198\

Of course, we want to change the dirty name “cakephp-cakephp-b81c198″ to our “project name”.

So we have to rename it and for this tutorial, we will name it “CakePhpProj”, awesome name right? If you don’t think so, you can choose the project name of your choice. Now we should have:

C:\wamp\www\CakePhpProj

Run CakePHP

Too early to run? You’re correct. This is just a test run. We just want to confirm if CakePHP can respond at this stage.

So to run CakePHP: Go to your browser > type “localhost/CakePhpProj“

You might see something beautiful like this:

using-cakephp-first-run

You might be disappointed or intimidated by now, but don’t worry, I’m at your side! Proceed to the next step below.

Configure CakePHP

Alright, so we’re going to address the issues on the previous screenshot, one at a time!

URL rewriting is not properly configured on your server.- Let’s start with this problem, this error is rare if you’re using a real hosting. But if you’re using localhost, here’s the fix:

1. On you notification area (lower right corner), click the WAMP icon.
2. Hover your mouse to the “Apache” folder
3. Hover your mouse to the “Apache modules” folder
4. Find and click “rewrite_module”, WAMP will automatically restart and check that apache module.

mod-rewrite-cakephp

After the fix, re-run our project on the browser, it should look like this now:

cakephp-fix-rewrite-mod

Please change the value of ‘Security.salt’ - To solve this, you have to to got the core.php file and just change the security salt string!

In my case, I have to open it in C:\wamp\www\CakePhpProj\app\Configcore.php

Find the word “salt” (Ctrl+F on your editor, I’m using notepad++)

You should see the line of code that looks like:

Configure::write(‘Security.salt’, ‘DYhG93b0qyJfIxfs2guVoUubWwvniR2G0FgaC9mi’);

Change the value to something like:

Configure::write(‘Security.salt’, ‘nowthisismyawesomesaltthatnoonecaneverknowxfs2gu’);

Then re-run the browser:

cakephp-fixed-security-salt

Please change the value of ‘Security.cipherSeed’ - The solution is the same with 2.4.2, just change the value and re-run!

cakephp-fixed-security-cypherseed

Your database configuration file is NOT present. – Now we have to make a database for our CakePHP application. If you need help doing that, here’s a guide: How To Create MySQL Database With PhpMyAdmin

After creating a database, we have to go again to the Config directory, in my case:

C:\wamp\www\CakePhpProj\app\Config

Now you should see a file named “database.php.default” and rename it to just database.php

After renaming it, we have to open it with our editor and supply the database details! In the $default array, what usually we have to change are the: database host, login (username), password and database

You would see something like this in default:

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

So we’re going to change it to:

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

Now, re-run our project page in the browser, it should look like this:

cakephp-installation-fix

Services

Still having hard time? I can personally help you do this for only $5!

That’s it for this post, enjoy and continue your CakePHP web development! Here is a continuation of this post: CakePHP Classes and Naming Conventions

Thanks for reading this How To Use CakePHP Framework, A Getting Started Guide!

Hi! I'm Mike Dalisay, the co-founder of codeofaninja.com, a site that helps you build web applications with PHP and JavaScript. Need support? Comment below or contact [email protected]

I'm also passionate about technology and enjoy sharing my experience and learnings online. Connect with me on LinkedIn, Twitter, Facebook, and Instagram.