In the previous blog we touched base with cakephp and how is serves as a fundamental tool to the internet. Today we are looking at a simple blog tutorial using cakephp 1.3.6 stable on a Linux operating system (Ubuntu 10.04).
Requirements to run cakephp smoothly on your system are :
- HTTP Server. For example: Apache. mod_rewrite is preferred, but by no means required.
- PHP 4.3.2 or greater. Yes, CakePHP works great on PHP 4 and 5.
Databases supported
- MySQL (4 or greater)
- PostgreSQL
- Microsoft SQL Server
- Oracle
- SQLite
Installing Cakephp
- Downloading a copy of Cake (zip/tar)
- Extract the zip/tar file to your local Dev environment (Commonly www/)
- Configuring your web server to handle php if necessary
- Checking file permissions
Set file permissions
- Open up your terminal (Applications/Accessories/Terminal)
- In you Terminal Navigate to where your cakephp folder is (“www/cakephp/”)
- enter “chmod 777 -R /app” and execute the command
- Navigate to www/cakephp/app/config/ and change the file “database.php.default” to “database.php”
- Open the file “database.php” and edit the database settings according to your machine
- Open the file core.php and look for “Configure::write(‘Security.salt’, ‘DYhG93b0qyJfIxfs2guVoUubWwvniR2G0FgaC9mi’)” and “Configure::write(‘Security.cipherSeed’, ‘92837462346523742348298347928’)” then change the both security codes to your own choosing
Before we move on your site is suppose to be looking something like this
Create your database schema we are using A simple table structure with three tables
CREATE TABLE `posts` (`id` int(11) unsigned NOT NULL auto_increment,`name` varchar(255) default NULL,`date` datetime default NULL,`content` text,`user_id` int(11) default NULL,PRIMARY KEY (`id`));
CREATE TABLE `comments` (`id` int(11) unsigned NOT NULL auto_increment,`name` varchar(100) default NULL,`content` text,`post_id` int(11) default NULL,PRIMARY KEY (`id`));
CREATE TABLE `users` (`id` int(11) unsigned NOT NULL auto_increment,`name` varchar(100) default NULL,`email` varchar(150) default NULL,`firstname` varchar(60) default NULL,`lastname` varchar(60) default NULL,PRIMARY KEY (`id`));
Naming conventions
Naming your files should be done according to your database tables. Controllers are always named in plural form using the name of each table followed by _controller all in lower letters using our example we will name all three controllers
users_controller.php
posts_controller.php
comments_controller.php
For our Model we simply name them according to each table in the database too
user.php
post.php
comment.php
Inside each Controller
We are going to add scaffolding to each controller so we can get the application up and running (Changing the names in bold “PostsController”,”Post” according to each controller “UsersController”,”User” etc)
<?
class PostsController extends AppController {
var $name = ‘Post‘;
var $scaffold;
}
?>
Inside each Model
<?
class Post extends AppModel {
var $name = ‘Post’;
}
?>
After adding all the controllers and models you can view your blog showing all records in your database with no css editing but cake has a default css that comes with the application.
http://localhost/cakephp/posts/
You now have a simple application running with CRUD