How to Build a CMS in CakePHP

Written by ankur taxali
  • Share
  • Tweet
  • Share
  • Pin
  • Email
How to Build a CMS in CakePHP
A content management system can help your business's website by making it easier to upload and delete content. (Jupiterimages/ Images)

For those looking to advance their programming skills, building a content management system is a great exercise to try out. You can build a CMS using the PHP framework called CakePHP. A content management system is nothing more than a piece of software that manages the material on a website. After you've built a CMS, a website's content can be added or removed with ease and without any further programming required.

Skill level:

Other People Are Reading

Things you need

  • CakePHP
  • Server with access to a database
  • Permission to modify your database

Show MoreHide


  1. 1

    Create a database called "Blog" by using PHPMyAdmin or SQL. Add a table called "Posts" to your database. Use the code below to create your table.


    'id' int(11) NOT NULL auto_increment,

    'title' varchar(50) default NULL,

    'content' text,

    'created' datetime default NULL,

    'modified' datetime default NULL,

    PRIMARY KEY ('id')


    Before moving forward, populate your table with example posts, like the ones given below.

    Id: 1

    Title: How to chop the perfect onion

    Content: Here is how to chop the perfect onion...

    Created: 2010-07-01 12:00:00

    Modified: 2010-07-01 13:22:14

    Id: 2

    Title: How to tie your shoes

    Content: What everyone should know how to do...

    Created: 2010-07-02 07:14:20

    Modified: 2010-07-02 10:22:34

    Id: 3

    Title: How to buy gum

    Content: Know what brands are the very best...

    Created: 2010-07-03 5:10:33

    Modified: 2010-07-03 6:30:14

  2. 2

    Go to the /models folder and create a new file called post.php. The /models folder is located in your /app directory. (CakePHP knows that post.php contains a model for the posts table.) Copy and paste the code below into the blank post.php.


    class Post extends AppModel {

    var $name = 'Post';



  3. 3

    Create a posts controller in the directory /controllers in your Cake installation. Start a new file. Call the file posts_controller.php. Open up the blank file, and paste in the following code.


    class PostsController extends AppController {

    var $name = 'Posts';

    function index() {

    $posts = $this->Post->find('all');





    A controller performs actions on all of the posts, so it uses the plural form ("posts"). The function index() finds all of the posts, and creates an array called $posts to use in templates.

  4. 4

    Create the corresponding index view. Views live in the /views folder, and views for the "Post" model live in views/posts/. Create a new file called index.ctp and paste the code below.


    <!DOCTYPE html>



    <title>My Blog</title>



    <h1>All Posts</h1>






    <th>Last Modified</th>



    <?php foreach ($posts as $post) : ?>


    <td><?php echo $post['Post']['id']; ?></td>

    <td><?php echo $post['Post']['title']; ?></td>

    <td><?php echo $post['Post']['content']; ?></td>

    <td><?php echo $post['Post']['modified']; ?></td>

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


    <?php echo $html->link('Delete', array('action'=>'delete', $post['Post']['id']); ?>



    <?php endforeach; ?>





    The foreach loop iterates through each array element, and does an echo. Before the loop closes, there are two $html->link objects. They provide an easy way to make HTML links.

  5. 5

    Go back to posts_controller.php. Open a new function brace for the add() method definition, and write down the following source code:

    $action Heading = "Add a Post!";


    if(!empty($this->data)) {


    if ($this->Post->save($this->data)) {

    $this->Session->set Flash(_('The Post has been saved', true));


    } else {

    $this->Session->set Flash(_('This post could not be saved.', true));



  6. 6

    Go to /views/posts and create a new file called add.ctp. ("ctp" stands for Cake template file.) Add the following code to the blank file.

    <h1>Add a post</h1>


    echo $form->create('Post', array('action'=>'add'));

    echo $form->input('title');

    echo $form->input('content');

    echo $form->end('Add a post');


  7. 7

    Complete the edit() action. The entire function definition is listed below. Copy and paste it in your file.

    function edit($id = NULL)

    if (empty($this->data)) {

    $this->data = $this->Post->read(NULL, $id);

    } else {


    $this->Session->setFlash('Post updated.');

    $this->redirect(array('action'=>'view', $id));



  8. 8

    Go to the views folder. Create a new view file called "edit.ctp". Paste the following code in the blank file.

    <h2>Edit Post</h2>


    echo $form->create('Post', array('action'=>'edit'));

    echo $form->input('title');

    echo $form->input('content');

    echo $form->input('id', array('type'=>'hidden'));

    echo $form->end('Edit Post');


  9. 9

    Copy and paste the delete method below in posts_controller.php.

    function delete($id = NULL) {


    $this->Session->setFlash('Post was deleted!');



Tips and warnings

  • Make sure you are comfortable with Models, Views and Controllers, and know what they do, before going further.

Don't Miss

  • All types
  • Articles
  • Slideshows
  • Videos
  • Most relevant
  • Most popular
  • Most recent

No articles available

No slideshows available

No videos available

By using the site, you consent to the use of cookies. For more information, please see our Cookie policy.