codeofaninja
website

How To Use CakePHP Framework, A Getting Started Guide!

Photo of Mike Dalisay
Modified Sunday, July 7, 2013
by - @ninjazhai

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

2.0 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

2.1 Download CakePHP (Step 1)


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.

2.2 Put CakePHP on Your Hosting (Step 2)


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


2.3 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/CakePphProj"

You might see something beautiful like this:


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

2.4 Configure CakePHP


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

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



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


2.4.2 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\Config\core.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:



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



2.4.4 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:


3.0 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

The Code of a Ninja Resources

For FREE programming tutorials, click the red button below and subscribe! :)
Thanks for the comments!
 
 
Fundamentals
"First do it, then do it right, then do it better."
~ Addy Osmani
"Talk is cheap. Show me the code."
~ Linus Torvalds
Let's Stay Connected!
g+ r
Android app on Google Play
© 2011-2014 The Code Of A Ninja. All rights reserved. Proudly Powered by Google Blogger. Images, logos, marks or names mentioned herein are the property of their respective owners.