Art AboutFAQWrite for usContact InfoRSS Feed
Search A/R/T:

An overview of the CakePHP framework

by Fabio Cevasco — Page 1 of 3
Click to rate:    
611 votes / avg. rating 28.68%

There are many frameworks available for the PHP programming language nowadays, and especially a lot of RAD (Rapid Application Development) frameworks which aim to make web development faster, less tedious and more organized. CakePHP was one of the first frameworks to port the RAD philosophy - which became so popular after Ruby on Rails - to the PHP programming language. CakePHP v1.0 is now one of the most popular and intuitive solutions for PHP programming, let's discover why...


 

The PHP programming language became popular because it allows developers to create powerful web applications in an easy way. PHP is dynamically typed, it was born as a procedural language and was originally conceived as a mere form interpreter - things were very simple then. Obviously as PHP's popularity grew, the developer community required additional functions and features to be included inside its core, and the latest PHP5 offers all possible web-development-related functionalities and even more, from basic form handling to platform-specific system functions. Even PHP has its weak spots of course, among which a sort of inherent disorganization both at core level (no namespaces, inconsistent naming conventions, etc.) and at user lavel: PHP code can be seamlessly integrated into HTML pages, and a lot of developers - newbies but also seasoned ones - have been using this feature to create the most tangled and messy examples of spaghetti code.

Indeed using a single php file for connecting to a database, fetching some results, doing some operations with them and then displaying them via a HTML table just works but this is not sufficient to make it a good coding practice. Imagine a lot of huge, monolitic scripts each including n others almost equally complex and tangled: the picture you get is just some very disorganized code which still works and maybe is even fast to run, but it's a real nightmare to maintain and debug.

One thing PHP code developers and Zend in particular didn't do until recently was try to bring order to the chaos and create an official framework offering some sort of structure and conventions to make PHP application easier to maintain and develop. It seems that finally something is happening with the Zend framework but perhaps is too late already: the new Zend framework is still under development while there are a lot of various alternatives out there which are stable enough to do wonders, and “CakePHP” is undoubtedly one of the most valid.

CakePHP - An easy way to organize your web project

CakePHP was originally born as an attempt to port the architecture and functionality offered by “Ruby on Rails”:http://www.rubyonrails.com/ to PHP, something which various other frameworks have tried to do in different ways. Although Cake has many similarities with the popular Ruby framework, it's currently evolving independently: it's not an actual port - that would be close to impossible, considering the features offered by Ruby itself which are not present in PHP - but rather a very PHP-specific implementation of Rails' architecture and RAD philosophy. CakePHP separates itself from the rest of the PHP world by concentrating on simplicity, portability, flexibility and scalability. CakePHP does not require any additional libraries like PEAR or Propel. CakePHP includes an architecture that allows for easily extending its many built-in features through components and helpers. All this in under 300K.

The only really important concept to grasp is its MVC (Model-View-Controller) architecture, which is actually common to many other PHP frameworks - including, recently, the Zend's. If you're new to this very intuitive and yet powerful concept, imagine to logically divide your web application in a few major sections (controllers), like a blog, a photo gallery and so on. Each section will obviously have access to one or more tables in your database (models) and will also include some template files (views) used to present content to the users. This may be a quite informal but concrete representation of the MVC architecture, using a real-world example. In other words, controller files handle your application's business logic, keeping it separate from the code used to represent the datasource (model) and the presentation layer (view). This separation may seem a bit too strict at first - you may be tempted to try including some business logic in your views, for example - but will be able to increase your code maintainability in the long term.

CakePHP can be freely downloaded from the official site and requires no installation at all: just extract the content of the compressed archive file into a public directory of your webserver to deploy the framework and your project's skeleton. The directory structure of CakePHP is designed to be easy to understand and well defined for intetlligent code seperation:

* app/ * config/ * controllers/ * models/ * plugins/ * tmp/ * vendors/ * views/ * webroot/ * cake/ * config/ * docs/ * libs/ * vendors/

To an extent, even someone who is not familiar with CakePHP can understand roughly where each particular file should be placed. There are three first level folders: cake containing the framework itself, app which hosts an empty project and vendors which can be used for third-parties libraries. It's worth noting that when creating a project with CakePHP you'll never have to touch the contents of the cake folder: the framework's source code is always kept separated by the user project. One of the most important part of Rails' philosophy Cake ported to PHP is convention over configuration, in fact there's no need for dozens of configuration files or no hidden settings to configure: the framework knows where everything is and it's smart enough to load the appropriate classes depending on a particular task, and of course a fixed directory structure plays a key role for this smart behaviour.

There are only two configuration files which can be modified, one is app/config/core.php and contains various application settings like the debug level, session timeout, etc., and the other - app/config/database.php - is necessary to establish a (persistent) database connection:

  1. <?php
  2. class DATABASE_CONFIG
  3. {
  4.     var $default = array('driver'   => 'mysql',
  5.                          'connect'  => 'mysql_pconnect',
  6.                          'host'     => 'localhost',
  7.                          'login'    => 'memouser',
  8.                          'password' => 'userpassword',
  9.                          'database' => 'memo' );
  10.     var $test   =  array('driver'   => 'mysql',
  11.                          'connect'  => 'mysql_pconnect',
  12.                          'host'     => 'localhost',
  13.                          'login'    => 'user',
  14.                          'password' => 'password',
  15.                          'database' => 'project_name-test');
  16. }
  17. ?>
Again, nothing too difficult to understand. Cake supports natively various commonly used databases like MySQL, PostgreSQL and SQLite, and uses a wrapper for database abstraction layers like AdoDb or PEAR::MDB. Once you edit this file, CakePHP will be fully functional and you'll be able to start developing your application.

 


 Tags:  , , , , , , ,
 Add tag(s) (comma separated):
[Tags beta] — [Add New][Help]  

Tags Help

Tags are keywords associated with a web page that help classifying information. You can find a good explanation here.

To add one or more tags to this page, simply enter them below (separate them with a comma) and hit enter or click on the "Go" button.

New Comment
This form allows you to type in a new comment. Keep in mind the following:
  • The system accepts input in plain text format. Newlines will be converted to the HTML equivalent, and the system will try to catch most URLs and make them clickable.
  • Your e-mail address will never be displayed. We will use it only to notify you when new comments are posted to this page.
  • As a rule, we do not delete comments unless they are offensive, racist, spam or otherwise inappropriate.
  • Bold fields are required
Your Name:
Your e-mail:
Type this number:
Subject:
Comment:
Comments   New Comment
Ok, but there are real aplications made with Cake? (#90)
By Salva Maine on 2006-05-30 18:50:57

Recentlty I looked in the Cake site, but I didn't find any real big aplication mede with it. Does this mean that there are not many developers working with it?
---
salva maine

[Reply to this]

Re: Ok, but there are real aplications made with Cake? (#91)
By John ZImmerman on 2006-05-30 20:47:10

Yes there are real applications being developed. Not everyone is posting their projects on CakeForge.

I am developing several applications that are very real. They are coming together very nicely.

The framework is still young and has just recently hit the 1.0 mark. 1.1 soon followed. 2.0 is in the works.

The applications will follow.

One of the developers runs his Blog and PhotoBlog on cake.

[Reply to this]

Re: Re: Ok, but there are real aplications made with Cake? (#92)
By John ZImmerman on 2006-05-30 20:48:42

It cut me off. :-(

Anyway you can see his blog at http://rd11.com and can download his code on http://cakeforge.org

[Reply to this]

Re: Ok, but there are real aplications made with Cake? (#94)
By Larry E. Masters on 2006-05-30 22:09:27

salva,

We have a very active community.
Our google group has over 1600 members and grows daily.

[Reply to this]

Re: Re: Ok, but there are real aplications made with Cake? (#95)
By Larry E. Masters on 2006-05-30 22:11:20

Cut me off also.

[Reply to this]

Re: Re: Ok, but there are real aplications made with Cake? (#96)
By Larry E. Masters on 2006-05-30 22:13:42
Try http://www.harro.com (#334)
By Harro Alive! on 2006-09-24 09:37:41

Its made with cake! :) Its at http://www.harro.com

[Reply to this]

Re: Ok, but there are real aplications made with Cake? (#379)
By Felix Geisendrfer on 2006-12-22 12:43:24

There are lot's of real and big apps that are developed with CakePHP. Right now I work for a client that does a CakePHP project with 200 MySql tables which also means there is an aweful lot of code in the application ; ).

[Reply to this]

Re: Re: Ok, but there are real aplications made with Cake? (#386)
By Tarique Sani on 2006-12-23 00:58:21

WOW! must be an awesome app ;)

[Reply to this]

Re: Re: Ok, but there are real aplications made with Cake? (#491)
By origo on 2007-03-03 10:08:45

The app I am working with now is 100% cake with about 50 models and 150 relations designed to execute millions of daily transactions. I am confident cake will stand up to the challenge.

[Reply to this]

Print Option? (#106)
By Mutant on 2006-06-01 10:44:06

It would be nice if these articles had a "print" option. Where I could print all 3 pages without comments. I really like reading these kind of articles over breakfast a long lunch, or in the John..

Mabey thats all the more reason to install a computer in my bathroom.

[Reply to this]

Re: Print Option? (#107)
By Marco on 2006-06-01 11:28:30

Don't fall into the PC-in-the-washroom trap... a friend of mine did just that and we haven't seen him since ;-)

We're working on a print option--give us a little time and you'll be able to do just what you want.

Cheers!


Marco

[Reply to this]

Re: An overview of the CakePHP framework (#112)
By I. Perez on 2006-06-04 13:24:07

Very Nice article indeed, however I beleive there is a misconception in the defenition of MVC present here, to my understanding and most of my professors the controller should not have business logic, but controll what views get present to the user and what methods of the model (containing the business logic) get executed. The model as well will have abstraction methods of the db.

[Reply to this]

Re: Re: An overview of the CakePHP framework (#117)
By nate on 2006-06-05 12:50:21

There are many different interpretations on how best to divide the labor among the tiers of MVC (all of which fall under the "MVC" umbrella), but the separation of primary concern, as Martin Fowler points out, is that of the View from the Controller and Model.

This of course depends on the context, but the general rule of thumb is that anything representing an abstraction of the data belongs in the Model, and everything else goes in the Controller.

Regarding the development of Cake, we generally let the DRY principle (and good common sense) dictate what should go where.

This, I think, represents the larger issue of the difference between development in the classroom, and development in the real world. You might want to double-check with your professor(s) that you're getting a well-rounded education. ;-)

[Reply to this]

Re: Re: Re: An overview of the CakePHP framework (#146)
By miami on 2006-06-22 09:46:40

Nate, your response is excellent. There are many different approaches and philosophies tought concerning MVC. Depending on the language and framework being used, certain approaches are more efficient.

Perez's approach is what is usually tought in Universities using Java and JSPs. I personally don't like it as much as cake's.

There is a difference between classroom assignments and real world application development. What you learn in school is one approach, but not necessarily the only correct approach.

Be open to learning new philosophies and techniques.

[Reply to this]

Re: An overview of the CakePHP framework (#160)
By sdozono on 2006-07-02 15:42:08

I translated this into Japanese.

[Reply to this]

Re: An overview of the CakePHP framework (#161)
By sdozono on 2006-07-02 15:44:30

cakephp.seesaa.net/article/20188537.html
cakephp.seesaa.net/article/20188537.html

[Reply to this]

Re: Re: An overview of the CakePHP framework (#367)
By reliable web hosting on 2006-11-20 19:39:52

What is required for cakephp? I don't see the requirements listed anywhere. Will it work with <A HREF=" http://hostbandit.com ALT="reliable web hosting">Reliable Web Hosting</A>

[Reply to this]

web design (#543)
By web development on 2007-05-24 19:52:45

I dont know, but it works here - http://venturait.com

[Reply to this]

Re: An overview of the CakePHP framework (#475)
By lou on 2007-01-16 22:33:57

Is anyone working on support for Firebird database?

[Reply to this]

Re: An overview of the CakePHP framework (#480)
By Ben on 2007-02-02 05:26:48

Listing 4, line 17, I guess this should be </code> and not <code> to split the two sections of code apart and have the page text in between.

[Reply to this]

CakePHP framework (#493)
By Sruthi Krishna on 2007-03-10 01:22:45

How to integrate cake php in projects.which file needs to include? Is anybody help me.?

[Reply to this]

cakePHP development services (#497)
By mar3k on 2007-03-20 10:01:01

Do you guys know of any cakePHP company for outsourcing large app project? Thanks

[Reply to this]

Re: cakePHP development services (#515)
By Peter Fogg on 2007-04-07 20:59:07

Contact me via e-mail or by telephone at 626.797.9125, my residence. We, Peter Fogg and Associates, are located southern California.

[Reply to this]

Re: Re: cakePHP development services (#544)
By cakephp Developer on 2007-05-27 14:48:05

Hi Peter Fogg .. can u get your mail?

[Reply to this]

Re: An overview of the CakePHP framework (#562)
By Greg on 2007-06-26 10:11:32

I've been working with CakePHP for a few weeks now and there are certainly some gotch'a expecially if you are used to RoR and expect things to work in that way. Watch out for using model::find() when cake wants model::read() - your saves then failsilently leaving you to work through masses of undocumented model code for a solution that does not exist.

If you want a really poorly documented framework then this is certainly for you. If you want something robust and intuitive that you can get up and running quickly then look elsewhere.

[Reply to this]

Re: Re: An overview of the CakePHP framework (#566)
By cakephp development on 2007-07-11 01:25:46

Ventura IT does cakephp development... contact them at http://venturait.com or 805 644 9981 or sales@venturait.com

[Reply to this]

Re: Re: Re: An overview of the CakePHP framework (#567)
By cakephp development on 2007-07-11 01:27:06
Re: Re: Re: Re: An overview of the CakePHP framework (#583)
By search engine optimization on 2007-08-21 00:11:20
how can i modify .htaccess (#611)
By harry on 2007-11-06 09:25:10

how can i modify my .htaccess file to change my url
my url mysite.com/shop?sn=abc&lb=xyz
to mysite.com/shop/abc/xyz

[Reply to this]

Index