Blog Tutorial in CakePHP


This week i was learnt cakePHP . how to create blog and learnt about auth component

(authentication) in cakePHP .

Create Blog using CakePHP

download cakePHP from http://cakephp.org/ and untar the file save it with project name(

for example: project directory name is my_project ) .

give permision to my_project/app/tmp

change filename of /app/config/database.php.default to  /app/config/database.php

and in file database.php give your mysql username and password .

Then create database name with your wish

$ CREATE DATABASE blog

then create table

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

Note: table name should be in plural

Now start to create blog using cakePHP

First when we type web address like

http://localhost/my_project/posts/

my_project is name of our project directory .

first it will go to dispatcher and it will go to controller .

First creating post model . Model will interact with databases

create file as my_project/app/model/post.php

Note : Model file should be singular form (i.e) post.php // not posts.php


<?php
class Post extends AppModel {
var $name = 'Post';
}
?>

2 . Creating post controller

create file as my_project/app/controller/posts_controller.php


<?php
class PostsController extends AppController {
 var $name = 'Posts';

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

}

?>

[\code]

<span style="color: #800000;">Note: here not singular , in controller name should be in plural (i.e) posts_controller.php</span>

if we enter address like below . it will call posts controller and calls index function index()

<span style="color: #800000;">localhost/posts/index</span>

<code>Now we did not created index page . index page will be in views</code>



 <h1>Blog posts</h1>
 <table>
 <tr>
  <th>Id</th>
 <th>Title</th>
 <th>Created</th>
 </tr>
 <?php foreach ($posts as $post): ?>
 <tr>
 <td><?php echo $post['Post']['id']; ?></td>
 <td>
 <?php echo $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>

this is index page of our blog.

If we click one of post we should view . so create a file in /app/views/view.ctp


<h1><?php echo $post['Post']['title']?></h1>
 <p><small>Created: <?php echo $post['Post']['created']?></small></p>
 <p><?php echo $post['Post']['body']?></p>

add an view action in controller .

going to add function view() in posts_controller.php


<?php
 class PostsController extends AppController {
  var $name = 'Posts';
 function index() {
 $this->set('posts', $this->Post->find('all'));
  }
  function view($id) {
 $this->Post->id = $id;
  $this->set('post', $this->Post->read());
  }

}

?>

Now we are ready to add post

create add action in posts_controller.php


<?php

class PostsController extends AppController {
 var $name = 'Posts';
  function index() {
  $this->set('posts', $this->Post->find('all'));
  }
  function view($id) {
 $this->Post->id = $id;
 $this->set('post', $this->Post->read());
  }
 function add() {
  if (!empty($this->data)) {
 if ($this->Post->save($this->data)) {
  $this->Session->setFlash('Your post has been saved.');
  $this->redirect(array('action' => 'index'));
  }
  }
  }
 }
 ?>

Now we need to add template for add  i.e in view file

app/views/add.ctp

 <h1>Add Post</h1>
 <?php
 echo $form->create('Post');
 echo $form->input('title');
 echo $form->input('body', array('rows' => '3'));
 echo $form->end('Save Post');
 ?>

Now we need Link for adding post

add following line in index.ctp


<?php echo $html->link('Add Post',array('controller' => 'posts', 'action' => 'add'

))?>

and adding function for delete in posts_controller.php


function delete($id) {
 $this->Post->delete($id);
 $this->Session->setFlash('The post with id: '.$id.' has been deleted.');
 $this->redirect(array('action'=>'index'));
}

Now delete link in index file (i.e) in app/views/index.ctp


<?php echo $html->link('Delete', array('action' => 'delete', $post['Post']['id']), null, 'Are you sure?' )?></code>

Finally for editing post , adding edit action in posts_controller.php


function edit($id = null) {
 $this->Post->id = $id;
 if (empty($this->data)) {
 $this->data = $this->Post->read();
 } else {
 if ($this->Post->save($this->data)) {
 $this->Session->setFlash('Your post has been updated.');
 $this->redirect(array('action' => 'index'));
 }
 }
}

and template for edit .

create file in app/views/edit.ctp


<h1>Edit Post</h1>
<?php
 echo $form->create('Post', array('action' => 'edit'));
  echo $form->input('title');
  echo $form->input('body', array('rows' => '3'));
  echo $form->input('id', array('type'=>'hidden'));
  echo $form->end('Save Post');
 ?>

add edit link in index.ctp


<?php echo $html->link('Edit', array('action'=>'edit', 'id'=>$post['Post']['id']));?></code>

Now blog was ready

Reference form cakePHP website : cakePHP blog tutorail

Regards

sathia

Advertisements

About sathia

Web developer at cloudmint
This entry was posted in Uncategorized. Bookmark the permalink.

One Response to Blog Tutorial in CakePHP

  1. That is a really good tip particularly to those fresh to the blogosphere.
    Simple but very accurate info… Appreciate your
    sharing this one. A must read post!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s