debuggable

 
Contact Us
 

Reusing Views for CRUD

Posted on 9/2/06 by Felix Geisendörfer

Deprecated post

The authors of this post have marked it as deprecated. This means the information displayed is most likely outdated, inaccurate, boring or a combination of all three.

Policy: We never delete deprecated posts, but they are not listed in our categories or show up in the search anymore.

Comments: You can continue to leave comments on this post, but please consult Google or our search first if you want to get an answer ; ).

As he did once before, tobius inspired me to write some code again. This time he was trying to make scaffolding more individual, so the first thing I pointed out to him was my blog post about using generateFields for using some of the scaffolding powers. Now one problem remained: He wanted to use the same views over and over again so I had an idea about how to reuse views in a simple and efficient manner.

Here is what I did:

app/app_controller.php

/* This is our AppController that will help us to use 4 common CRUD views */
class AppController extends Controller
{                  
    function render($action=null, $layout=null, $file=null)
    {          
        if (!isset($action) && in_array($this->action, array('create', 'update', 'read', 'delete')))
        {
            parent::render('../crud/'.$this->action, $layout, $file);
        }            
        else
        {
            parent::render($action, $layout, $file);
        }
    }
}

app/items/items_controller.php

/*
    This is a sample items controller with basic CRUD functions
*/

class ItemsController extends AppController
{    
    var $uses = array(); // This test controller doesn't use db
   
    function create()
    {
        // This will use app/views/items/create.thtml
        $this->render('create');
    }
   
    function read()
    {
       // This will use app/views/crud/read.thtml
    }
   
    function update()
    {
       // This will use app/views/crud/update.thtml        
    }
   
    function delete()
    {
       // This will use app/views/crud/delete.thtml                
    }
   
}

I hope it's useful to somebody out there ; ).

 
&nsbp;

You can skip to the end and add a comment.

This post is too old. We do not allow comments here anymore in order to fight spam. If you have real feedback or questions for the post, please contact us.