CakePHP Framework Tutorial

Ranked #2,588 in Internet, #149,588 overall

Introduction to the CakePHP Framework

CakePHP is a free, open-source, rapid development framework for PHP. It allows programmers to create web applications more rapidly. It is modeled after the concepts of Ruby on Rails. PHP programmers are now expected to be expert in one of the popular frameworks. The tutorial below which is based on the one on the CakePHP website is designed to be as simple as possible, it will show that a few lines of code is sufficient to create a simple application.

CakePHP Hot Features

  • No Configuration - Set-up the database and let the magic begin
  • Extremely Simple - Just look at the name...It's Cake
  • Active, Friendly Community
  • Clean IP - Every line of code was written by the CakePHP development team
  • Best Practices - covering security, authentication, and session handling, among the many other features
  • Object Oriented - Whether you are a seasoned object-oriented programmer or a beginner, you'll feel comfortable

Installing CakePHP

Download the latest stable version of CakePHP from the link below. Currently the latest version 1.3.10.

Unzip the file and FTP to your website.

There is no setup except for entering your MySQL connection details. I also had to configure my Apache Server for

LoadModule rewrite_module modules/mod_rewrite.so # uncomment in httpd.conf

#Also in httpd.conf
Also find AllowOverride None
Should be:
AllowOverride All
cakephp.org
Official Cake PHP website

CakePHP Create a Simple Blog Example

CalePHP Hello World Example

This is a simplified version of the example on the CakePHP website. It does the least possible for simplicity it will just list your blog postings

Stage 0) Set up your Data

MySQL

/* Use PHPMyAdmin or equivalent to set up your data */

CREATE TABLE posts (
id INT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
title VARCHAR(50),
body TEXT,
created DATETIME DEFAULT NULL,
modified DATETIME DEFAULT NULL
);

/* insert some posts for testing: */
INSERT INTO posts (title,body,created)
VALUES ('The title', 'This is the post body.', NOW());
INSERT INTO posts (title,body,created)
VALUES (Squidoo useful for tutorials', 'Learn about Squidoo', NOW());
INSERT INTO posts (title,body,created)
VALUES ('Another Title', 'Put what you like here', NOW());

Stage 1) Create your Model

<?php
// /app/models/post.php
class Post extends AppModel
{
var $name = 'Post';
}

?>

Stage 2) Create Your Controller

<?php
// /controllers/posts_controller.php
class PostsController extends AppController
{
var $helpers = array ('Html','Form');
var $name = 'Posts';

function index() {
$this->set('posts', $this->Post->find('all'));
}

}
?>

Stage 3) Create Your View

<!-- File: /app/views/posts/index.ctp -->

<h1>Blog posts</h1>
<table>
<tr>
<th>Id</th>
<th>Title</th>
<th>Created</th>
</tr>

<!-- Here is where we loop through our $posts array, printing out post info -->

<?php foreach ($posts as $post): ?>
<tr>
<td><?php echo $post['Post']['id']; ?></td>
<td>
<?php echo $this->Html->link($post['Post']['title'],
array('controller' => 'posts', 'action' => 'view', $post['Post']['id'])); ?>
</td>
<td><?php echo $post['Post']['created']; ?></td>
</tr>
<?php endforeach; ?>

</table>

Stage 4) Run your Blog

http://www.yoursite.com/posts/index/

Which PHP Framework do you use Poll

Loading poll. Please Wait...

Other PHP and Technical Lenses

Loading

MVC : Model View Controller

What does MVC Mean?

(Model View Controller) MVC expresses the separation of a software architecture into three distinct elements. The 'Model' is how the underlying data is structured. The 'View' is what is presented to the user or consumer. The 'Controller' is the element that performs the processing.

The model manages the behavior and data of the application domain, responds to requests for information about its state (usually from the view), and responds to instructions to change state (usually from the controller). In event-driven systems, the model notifies observers (usually views) when the information changes so that they can react.
The view renders the model into a form suitable for interaction, typically a user interface element. Multiple views can exist for a single model for different purposes. A viewport typically has a one to one correspondence with a display surface and knows how to render to it.
The controller receives user input and initiates a response by making calls on model objects. A controller accepts input from the user and instructs the model and viewport to perform actions based on that input.
  • Model
    The model manages the behavior and data of the application domain, responds to requests for information about its state (usually from the view), and responds to instructions to change state (usually from the controller). In event-driven systems, the model notifies observers (usually views) when the information changes so that they can react.
  • The View
    The view renders the model into a form suitable for interaction, typically a user interface element. Multiple views can exist for a single model for different purposes. A viewport typically has a one to one correspondence with a display surface and knows how to render to it.
  • The Controller
    The controller receives user input and initiates a response by making calls on model objects. A controller accepts input from the user and instructs the model and viewport to perform actions based on that input.

CakePHP Websites and Links

cakephp.org
CakePHP Official Site
CakePHP Documentation
lWelcome to the Cookbook, the CakePHP documentation. The Cookbook is a wiki-like system.
CakePHP Training Videos
CakePHP TV - Official Site for All CakePHP Related Training Videos

CakePHP 1.3 Application Development Cookbook

This book gets very good reviews, particularly from procedural programmers who are only just learning the power of the MVC Model and Object Orientated Design.

CakePHP 1.3 Application Development Cookbook

Amazon Price: $38.01 (as of 05/30/2012)Buy Now

CakePHP Reader Feedback

What is your experience of Cake PHP or other frameworks

Examples of my successful Squidoo Lenses

Loading

by

thesuccess

> 101 Keyword Research Tips

> 101 SEO Tips to get more Traffic

> 101 Handy Tips

> 101 Success Tips

> 101 Fun Quizzes
more »

Feeling creative? Create a Lens!