Cake PHP

Getting Started With CakePHP

CakePHP is an open-source framework for PHP intended to make developing, deploying and maintaining applications much easier. CakePHP offers many useful design patterns, such as the Model-View-Controller pattern, seen in other popular frameworks like Ruby On Rails. The CakePHP framework also provides a slew of valuable reusable libraries for dealing with common tasks. An example is “Inflector” (a routine that takes a string and handles pluralization).

This guide will attempt to point you in the right direction so you can get started with CakePHP and eventually develop your own applications using it.

Step 1: Why CakePHP?

A significant amount of development time with PHP is spent rewriting common code for routine operations such as database access or returning data to the browser. Of course, all this routine code can quickly become disorganized in traditional PHP applications. What is needed is a framework for PHP that does what Ruby On Rails did for Ruby.

CakePHP has been around for awhile and does exactly that. It provides a number of useful libraries in support of common tasks and includes facilities for organizing code in folders and associating code with files. As a result, time spent writing and organizing code becomes greatly reduced.

Below are just a few things CakePHP offers to make development easier.

  • Free Open-Source MIT License allowing you to use CakePHP applications within any of your own projects.
  • Compatibility with both PHP4 and PHP5. The minimum version needed is 4.3.2.
  • Support for MySQL, PostgreSQL SQLite, PEAR-DB and wrappers for ADODB, a database abstraction library.
  • Model-View-Controller layout.
  • Easy CRUD (Create, Read, Update and Delete) database interaction.
  • Scaffolding to save production time.
  • Search Engine Friendly URLS.
  • Input validation and sanitization tools to make your applications much more secure.
  • Templating with familiar PHP syntax.
  • Caching Operations.

Step 2: Download The Framework

Before you start developing with CakePHP you will need your own copy of the framework uploaded to your server. Visit CakePHP.org and click the large “Download” button. Make sure to download the stable release and not the release candidate. There are also many different file formats available so you can pick the best one for your computer.

Step 3: Uploading and Understanding the File Structure

Once you have your fresh copy of CakePHP out of the oven, the next step is to upload the copy to a PHP and MySQL enabled web space. I would recommend creating a new directory for CakePHP projects.

Once the upload has finished the directory structure should look something like this:

/path_to_root_folder
	/cake/
	/docs/
	/app/
    
    		config/
		controllers/
		models/
		plugins/
		tmp/
		vendors/
		views/
		webroot/
		index.php
		.htaccess
        
	/vendors/
	index.php
	.htaccess

All these directories or folders may look a little daunting at first, but the separate directories are meant to better organize all of the framework components. Since names like “tmp” aren’t self-explanatory here is what these folders are for:

  • The cake folder stores all the core functions and internals for CakePHP. You will usually not need to edit anything here.
  • The docs folder contains very little, but does hold the license information (COPYING.txt), a change log and some other useful files. This directory is not important for CakePHP to run so you can remove it if you wish.
  • The app folder is where your application code will go. The app folder will hold your controllers, configuration, templates and much more.
    • The config folder contains all the configuration files for the application. This includes database details, access list, inflections and routes (URL rewriting).
    • The models folder stores all the sql database functionality for your application.
    • The views folder stores all the templates, layouts (header, footer) and helper modules that assist functionality (such as AJAX).
    • The controllers folder stores all the controllers for your application. A controller is the part of the application that directs and controls the model and the views by accepting input and deciding what to do with it.
    • The plugins folder stores plugins which are a combination of models, views and controllers that can be packaged and used in other applications. Examples are user management modules or an RSS module.
    • The tmp folder stores cache files generated by the caching system and also stores debugging logs. This folder will be very valuable during development.
    • The vendors folder, can contain other libraries that you want to include in a particular application.
    • The webroot folder stores static media such as CSS, images and the JavaScript needed by your application.
  • The second vendors directory will allow you to store third-party libraries and hook into them from your CakePHP controllers. For example if we wanted to built a Facebook application with CakePHP we could drop in the Facebook library and configure CakePHP to load it.

Step 4: Configuring CakePHP

Configuring CakePHP is pretty straightforward. We just need to tell CakePHP our database details and set up how we want the core functionality to work.

For development purposes you should create a new database and a user with the following privileges: ALTER, CREATE TEMPORARY TABLES, CREATE, DELETE, DROP, SELECT, INSERT, UPDATE, REFERENCES, INDEX, LOCK TABLES.

Once the user and database have been created, we can find CakePHP’s database configuration file, located in /app/config/database.php.default

Open and scroll down to the following array

var $default = array('driver' => 'mysql',
			'connect' => 'mysql_connect',
			'host' => 'localhost',
			'login' => 'user',
			'password' => 'password',
		        'database' => 'project_name',
			'prefix' => '');

and fill in your database details as necessary. If for some reason you cannot create a new database, or your host does not allow it, you can set a table prefix for all your CakePHP tables by setting a value in the ‘prefix’ index. Make sure to rename this file to /app/config/database.php

More core configuration is located in /app/config/core.php. You can change the level of debugging information, how sessions are stored, session time outs for security, and the names of cookies. Once we start developing we may need to adjust these, but the defaults are fine for most needs.

Step 5: Making Sure it Works

Once you have entered the correct database details and uploaded all the CakePHP files, the installation should be ready for development. Point your browser to the folder that you uploaded the installation to. If everything is working, you should see the following success page:

Closing

This tutorial was meant to introduce the basics of CakePHP and how to get it up and running on your server. In future installments we will look at developing an application from the ground up using CakePHP, adding effects such as AJAX, and integrating CakePHP with other libraries and services such as Facebook.

  • Subscribe to the NETTUTS RSS Feed for more daily web development tuts and articles.


Tags: cakePHP
Note: Want to add some source code? Type <pre><code> before it and </code></pre> after it. Find out more
  • http://www.designbykai.nl Kai

    Im new to PHP framework and stuff and I was looking for this!

    Thank you very much!

  • http://resnodesigns.com Bryan P.

    I have done some searching of my own, looking at different frameworks for PHP. I have noticed some seem to get my “media” attention. Out of curiosity why did you choose to cover CakePHP instead of another framework?

  • http://www.scheetzdesigns.com Justin

    Looking forward to the rest of the series!

  • http://www.johnwilsononline.com John

    Great stuff; as well as following the Create Your Own PHP Framework tutorials (Where’s the next one???) this will be a great series to follow. Keep up the good work NetTuts!

  • http://www.ben-griffiths.com Ben Griffiths

    This should be a great series – CakePHP is brilliant to use, but it can be very daunting to those new to frameworks :)

  • chris simpson

    CakePHP is a great ruby-on-rails esque framework, great to have a series on here where people can learn it.

    Great work!

  • http://insicdesigns.com insic

    hmmm another series. and now its cake. nice.

  • http://cyberantix.org Connor

    Good Job Justin! This looks like a good series.

  • Salman

    Great stuff

    thanks for the tut

  • http://barttos.net/ Barttos

    Nice post, but frameworks is not so good for professional developing!

    • Carl Marshall

      Are you sure about that. 90% of web development projects are utilizing a third party framework. How long have you been developing?

  • http://www.ben-griffiths.com Ben Griffiths

    Barttos – what do you mean? – frameworks are fantastic for professional development!

  • Dan

    Cool, I tried this once and failed. I think I’ll give it another go.

  • Sean

    CodeIgniter > CakePHP

    CI has miles better documentation.

  • Jeremy

    I had to use cakephp for a project and at first it kinda drove me nuts because it seemed to make things harder, but once you actually learn how to use it, I LOVE MVC now and try to do any project that I can in it. Everyone needs to give it a chance

  • http://www.kevinquillen.com Kevin Quillen

    This is one of the better code frameworks out there. I wish we were using more of it. Nice.

  • http://www.darrenmcpherson.co.uk Gafroninja

    I haven’t tried CakePHP, because I’m still in love with Code Igniter.

  • Chris

    Great intro, looking forward to the next article!

    Barttos, I have to agree with Ben – most frameworks are great for professional development. Cake (and others) are used by lots of web pros.

  • Sabandija

    Well I think it’s nice to have many tutorials going on here at NETTUTS but I’d prefer to have less and be able to focus on them instead, thanks anyway for the goods tuts you have here guys.

  • http://www.furleydelphia.com Furley

    YAY! I have been asking for this since this site started. Can’t wait for future installments.

  • http://joshmmiller.com Josh

    I’m fairly new to PHP in general and have had no experience with frameworks. I’ve been following along with the create your own PHP frameworks and have enjoyed this tutorial. Would creating your own framework be better than using an established framework such as CakePHP?

  • http://www.furleydelphia.com Furley

    @Josh – Cakephp is a more developed framework with a great community of support and documentation, than a custom framework. You just wont get that with your own custom framework, but being new to php you may want to play around with your own framework to familiarize yourself with the language. You may ultimately decide that no framework best suits you or your project, but you will learn alot. I think it depends on your project.

  • http://benmillsdesigns.com Ben Mills

    I was just thinking about how it would be nice to see some Cake on NETTUTS this morning, and when I got to work and checked there was this article!

    Great work Justin!

  • http://insicdesigns.com insic

    I agree with Sean. I love CI

    • http://flik.elance.com Flik

      Why you love with CI.

      I can prove bug in CI.

  • http://insicdesigns.com insic

    @Barttos I hope you don’t mean with your comment about frameworks. Else you are wrong.

  • http://janckos.net Janckos

    Desearia ver por aca tutoriales de CakePHP.
    Buen post, felicitaciones.

  • http://barttos.net/ Barttos

    Ben, Chris, insic, this is only my opinion! I use frameworks (ZF) only when dead-line is near me :)

  • http://www.kevinquillen.com Kevin Quillen

    Frameworks are a must to keep your projects organized and clean. They allow for rapid development of web applications and are a godsend in a group production environment.

  • http://www.davidrojas.net David Rojas

    About time! :-)
    Thanks, Justin. I’ve been waiting for a cakephp tutorial here for a long time.

  • Abdullah Al-Ageel

    Seems a god series to follow .. count me as a follower.

  • Patrick

    hm… i’ve tested some frameworks out, but i don’t use them, they’re mostly (much) slower than my own code. And, the really bad thing of a framework like CI or Cake – you don’t learn really much about PHP and how it works. That’s in my opinion a really, really bad thing… they are helpfull, when a deadline is too short to you, but it’s not a better way to code.

  • http://laminbarrow.com Lamin Barrow

    Man.. CakePHP is good. I am keenly looking forward to the rest of the tuts.

  • sloser

    @Barttos frameworks are ONLY solution for professional developing, IMHO

    If you use it on your every project, dead-lines will be not be threat any more ;)

  • http://www.vileworks.com Stefan

    Waw, you got the exact next thing on my to-learn list: “A PHP framework, probably Cake”!

  • http://armandososa.com Armando Sosa

    Hey! Great tutorial, I’m already an old time Cake Lover (I did the site) and my advice will be: Download the 1.2 release candidate. Altough it’s a .2 release, the differences between 1.1 and 1.2 are HUGE. 1.1 is good, but once you use 1.2 you’ll have no doubt that CakePHP is the best framework out there.

    I know that 1.2 stable is about to be released, but the RC is stable enough. Anyway, you are still learning don’t you?

    I’m looking forward to this series.

    (Btw, as you can see, English is not my first language. Sorry for that)

  • http://cakebaker.42dh.com Daniel Hofstetter

    Like Armando I recommend to use CakePHP 1.2, even though it is still a RC.

  • http://www.jashsoft.com Jash Sayani

    Wow! This is just what I was looking for! Thanks a lot !

  • http://abhinavsingh.com/blog Abhinav Singh

    Excellent stuff

    For those interested in Symfony, here is an excellent tutorial:
    http://abhinavsingh.com/blog/2008/10/getting-started-with-symfony-a-php-framework-part-1/

  • Juan

    Hey man, you should try 1.2, i think telling people to not download it is a bit misleading, 1.2 is stable enough for almost every project, also 1.1 is almost dead, and all the developing and documentation efforts are now spend in 1.2.

    And they are very different, a lot of things have change to a better way. also 1.2 is not new stuff, it has been on developing like for a year (as far as i have tracked the project) but it seems to me, that the core team want to make it perfect to release it as a proper stable.

    (English is not my first language)

  • http://dalsvaag.net Christian Dalsvaag

    Barttos: Using a framework like CakePHP, CodeIgniter and (Rails if you’re on Ruby) is GREAT for professional development. This actually is what professional development is today. None of the big companies write code from scratch. People rely mostly on WordPress and even Joomla! for CMS. This is just a better way of rapid development.

    Patrick: Are you serious? Well, when you are a professional developer I bet you don’t need to learn from writing code. I’m not saying we’re not learning, we’re always learning new things. But of course you are learning! Do you really know what a PHP framework is? When you use it, you TYPE PHP. Object Oriented Programming > Non-OOP. I can agree that it might be slower, but development is more rapid!

  • http://barttos.net/ Barttos

    @Christian, “None of the big companies write code from scratch.”… – really? Every big companies have her own framework for anykind of projects.

  • http://dalsvaag.net Christian Dalsvaag

    Barttos: Well, now – of course they make their own frameworks too. But I was talking about writing code from scratch for every project. Anyway, as I were saying – most companies rely on already-finished frameworks; and when I say that I’m not talking about all countries, but at least in Norway.

  • http://www.image2markup.com Ivan

    I was expecting so much more from the first Cake tutorial. Shame.

  • http://www.pixelsoul.com pixelsoul

    Lets try not to get a framework and something like WordPress confused. WordPress is not a framework.

    I think that if you are new to PHP you should get your hands dirty at first and learn the basics but in the same breath you can learn a little OOP if you work with Cake. I think frameworks though are great for rapid development in a professional environment, especially when you have more then one developer working on the the project and I do know that a lot of companies use them and/or build their own for consistency if not for anything else.

  • http://www.freshclickmedia.com Shane

    Using frameworks is a bit of a no-brainer – use what you’re familiar with, and what works for you :)

  • John Rico

    Frameworks are just a way to abstract some ideas but they will keep you on one and only one way to do the things.

    I think is better the Zend Framework approach as modules decoupled or components and you could still follow the MVC pattern or another way. I’m agree with some posters that frameworks are not always the best answer. If you follow good practices, patterns and your PHP code its OOP, there will be no problems even without a framework and you could get better performance without layers and layers of abstraction that frameworks impose to you.

    Frameworks are good for RAD but not so much of Flexibility.

    2c.

  • Pingback: Il meglio della settimana 3 - MaiNick Web

  • http://www.renderrobot.com alex

    Looking forward to getting my teeth into this

  • http://www.blueemberdesign.com Garrett St. John

    Just getting my feet wet with web development using CakePHP. I’m really looking forward to reading the upcoming tuts!

  • Jonny

    I’m still skeptical. I’ll download it and try it out, but as a long-time PHP/MySQL user, i always find myself going back to coding things the ‘old-fashioned’ long way. I’m not anti-change… I’ve found Jquery to be an Excellent tool for me, and now use it extensively, but when it comes to server side scripting, I always find myself needing the flexibility that doing it my own way offers.

  • http://www.kevinquillen.com Kevin Quillen

    Have you done a lot of large scale projects with multiple people involved?