CakePHP Installation Guide: Just Add Water and 2 Medium Eggs

Introduction

As you guys know, I’ve recently discovered the marvels of rapid web development with CakePHP. This shall be the first in a long serious of CakePHP related material coming from me since I’ve now veered off on a slightly different focus.

After a short analysis, I’ve picked CakePHP as my framework of choice. I’ve been struggling through it for a few months now, but it’s by far better than the alternative of coding by hand (Yeah, I know I’m lazy). I’ve struggled through it so that you don’t have to.

Downloading – Stable or Alpha Version

Step one is downloading CakePHP. The first problem that you’ll run into is that there are two (2) versions to pick from: Stable 1.1 and Alpha 1.2. When I first started out, I needed to use CakePHP for a project instantly, so I decided to with the stable version because the word alpha scared me a little bit. I mean, it’s not even beta yet.

That was my big mistake. A lot of seasoned CakePHP developers will tell you that the alpha version is ready to go and can (and has been) used in production environments. There is of course the slim chance that some key function or component might change slightly, and you application may break on an upgrade for the alpha version, but that’s a chance that I’m willing to take. The alpha version has a lot more in place (emailing templates, authentication, pagination of results, etc), that it just makes everything worthwhile. Also, a lot of the newer tutorials you’ll find make specific reference to version 1.2. And let’s face it, version 1.2 is just way cooler.

If you’re just starting out, I still recommend the alpha version 1.2, because by the time you become well versed and need to crank out a web application or site version 1.2 may well be stable.

Upload to Server

One of the key things I was looking for with a PHP framework is compatibility with PHP 4.0. Some of my clients are on older servers and it’s harder to get someone who is already paying for a host to switch just because you said so. Here are the server requirements from the CakePHP manual:

  • An HTTP server (like Apache) with the following enabled: sessions, mod_rewrite (not absolutely necessary but preferred)
  • PHP 4.3.2 or greater. Yes, CakePHP works great in either PHP 4 or 5.
  • A database engine (right now, there is support for MySQL, PostgreSQL and a wrapper for ADODB).

The manual recommends various methods of installation for development purposes and production environments. These recommendations are all well and good if you have your own server, but some things are simply not configurable with the shared hosts that most of us use these days.

I personally don’t see a problem with the development setup. The take the CakePHP folder and dump it the the www root of the site (if it’s the main site of course) or a subdirectory. The one tweak I would make, if your host allows, is to point your www root for that domain to the app/webroot/ folder.

These are recommended for security purposes, but I haven’t seen a problem with any installation that I’ve had. As a matter of fact, this is one of the few times that I’ve heard someone recommend this type of setup. Even well established packages like WordPress, SMF, Zen Photo, phpBB, etc just need you to dump files in a folder.

There is something to be said about the recommended setup though. If you happen to run multiple installations of CakePHP and have different applications, in theory, all of them can share the same CakePHP base code and you’d have different application folders. But for right now, this is just a get up and get started guide. The only thing that you need to ensure is that the app/tmp directory and all subdirectories are writable.

Configuration

Everything you’ll need is in the app/config folder. The file core.php houses, well, core configuration settings. Here are a few of the main ones:

  1. define('DEBUG', 3);

    set’s the debug level. Use 0 for production

  2. define('CAKE_ADMIN', 'admin');

    You’re almost always going to need this, so you might as well uncomment it now. This sets up admin routing so you can have urls like site.com/admin/events/

  3. define('CAKE_SESSION_STRING', 'DYhG93guVoUubWwiR2G0FgaC9mi');

    A random string used for CakePHP sessions. You need to change this into something unique or you might potentially leave holes in your app

Then we have the database.php file. Modify the following to suite your database configuration:

var $default = array(
'driver' => 'mysql',
'persistent' => false,
'host' => 'localhost',
'login' => 'username',
'password' => 'passowrd',
'database' => 'database name',
'prefix' => ''
);

I personally have a host that limits the number of MySQL databases that you’re allowed, so I’m forced to dump everything into one database. This makes the prefix option invaluable to me.

Lastly we have routes.php. This file is used to for URL routing. Consider the following:

Router::connect('/', array('controller' => 'contents', 'action' => 'view', 'home'));

This is going to route the homepage site.com/ to my contents controller and run the view action and pass to this the parameter home. This is the same as going to: site.com/contents/view/home/. We can also do some other marvelous things with routing, but I’ll save those for later.

Depending on what your setup is and where you host, you might have to make modifications to the .htaccess file. I haven’t narrowed down an explanation yet, but on various shared hosts, adding “RewriteBase /” solves a lot of problems, just keep that in mind.

Read more on the CakePHP manual.

Launch

Launch CakePHP and make sure your start up screen comes up. Then fire away are your Models, Views and Controllers.

Comments

  1. I just started with CakePHP this past weekend as well. How about you keep blogging and I’ll just learn from you?! Hehe, kidding. Good post, thanks!

  2. I’m trying to make this a “For Dummies” CakePHP blog (no offense to the newbies).

  3. hi Baz L, im trying to use Cakephp 1.2 alpha, but there are some problems.
    i’ve built blog demos application on cakephp 1.1, there was no problem. But on cakephp 1.2, i met with difficulties. I put the project into cake12 folder like this: cake12/app,cake,vendors, and the same with cake 1.1, folder cake11 contained app,cake,vendors folder. Both were put into the web root folder. As i run by these urls:
    http://localhost/cake11/ and http://localhost/cake12/, the first page blog of both were right loaded, but when i clicked on link “Add post”, it just appeared right url for cake 1.1(was http://localhost/cake11/index.php/posts/add/) and wrong for cake 1.2(was http://localhost/posts/add/). Do you know why it’s wrong? I used the same $html->link(“Add Post”, “/posts/add”) for two. And do you you know how to change the url to http://localhost/cake11/posts/add/ instead of http://localhost/cake11/index.php/posts/add/ ?

  4. sir when i installing cakephp i got these errors please send answer to [email protected]

    Warning (2): mysql_connect() [function.mysql-connect]: Can’t connect to MySQL server on ‘localhost’ (10061) [CORE\cake\libs\model\datasources\dbo\dbo_mysql.php, line 117]

    Warning (2): mysql_select_db(): supplied argument is not a valid MySQL-Link resource [CORE\cake\libs\model\datasources\dbo\dbo_mysql.php, line 122]

    Warning (2): mysql_get_server_info(): supplied argument is not a valid MySQL-Link resource [CORE\cake\libs\model\datasources\dbo\dbo_mysql.php, line 130]

    Warning (2): mysql_query(): supplied argument is not a valid MySQL-Link resource [CORE\cake\libs\model\datasources\dbo\dbo_mysql.php, line 152]

  5. HIral Vyas says:

    Hi

    I download cakePHP 1.2.2.8120. from cakephp.org site.

    Now can you give me the steps for changing root path and all such things.

    Thanks in advance

  6. i’m trying to start cakephp as my framework ,so I want to install cakephp in my wampserver but it is not working.I had unzip the cakephp version cake_1.2.5 as cake in my wampserver as c:\wamp\www\test\cake.i had connect to the databse also but i got the below problem

    CakePHP: the rapid development php framework

    Release Notes for CakePHP 1.2.4.8284.

    Read the changelog
    Notice (1024): Please change the value of ‘Security.salt’ in app/config/core.php to a salt value specific to your application [CORE\cake\libs\debugger.php, line 557]
    Your tmp directory is writable.

    The FileEngine is being used for caching. To change the config edit APP/config/core.php

    Your database configuration file is present.

    Cake is able to connect to the database.

    Editing this Page

    To change the content of this page, create: APP/views/pages/home.ctp.
    To change its layout, create: APP/views/layouts/default.ctp.
    You can also add some CSS styles for your pages at: APP/webroot/css.

    Getting Started

    new CakePHP 1.2 Docs

    The 15 min Blog Tutorial
    More about Cake

    CakePHP is a rapid development framework for PHP which uses commonly known design patterns like Active Record, Association Data Mapping, Front Controller and MVC.

    Our primary goal is to provide a structured framework that enables PHP users at all levels to rapidly develop robust web applications, without any loss to flexibility.

    Cake Software Foundation
    Promoting development related to CakePHP
    CakePHP
    The Rapid Development Framework
    CakePHP Documentation
    Your Rapid Development Cookbook
    CakePHP API
    Quick Reference
    The Bakery
    Everything CakePHP
    The Show
    The Show is a live and archived internet radio broadcast CakePHP-related topics and answer questions live via IRC, Skype, and telephone.
    CakePHP Google Group
    Community mailing list
    irc.freenode.net #cakephp
    Live chat about CakePHP
    CakePHP Trac
    For the Development of CakePHP (Tickets, SVN browser, Roadmap, Changelogs)
    CakeForge
    Open Development for CakePHP
    Book Store
    Recommended Software Books
    CakePHP gear
    Get your own CakePHP gear – Doughnate to Cake

    (default) 0 query took ms
    Nr Query Error Affected Num. rows Took (ms)
    SO ANYONE HELP ME PLEASE

Trackbacks

  1. […] of tutorials and guides on how to get CakePHP set up on the web. I did a quick search and found this one to be quite good, but there are plenty more out there. The most common newbie gotcha I find is […]