Reusing Views for CRUD

Posted by Felix Geisendörfer, on Feb 09, 2006 - in PHP & CakePHP » Controllers, Components & Shells

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

php
  1. /* This is our AppController that will help us to use 4 common CRUD views */
  2. class AppController extends Controller
  3. {                  
  4.     function render($action=null, $layout=null, $file=null)
  5.     {          
  6.         if (!isset($action) && in_array($this->action, array('create', 'update', 'read', 'delete')))
  7.         {
  8.             parent::render('../crud/'.$this->action, $layout, $file);
  9.         }            
  10.         else
  11.         {
  12.             parent::render($action, $layout, $file);
  13.         }
  14.     }
  15. }

app/items/items_controller.php

php
  1. /*
  2.     This is a sample items controller with basic CRUD functions
  3. */
  4. class ItemsController extends AppController
  5. {    
  6.     var $uses = array(); // This test controller doesn't use db
  7.    
  8.     function create()
  9.     {
  10.         // This will use app/views/items/create.thtml
  11.         $this->render('create');
  12.     }
  13.    
  14.     function read()
  15.     {
  16.        // This will use app/views/crud/read.thtml
  17.     }
  18.    
  19.     function update()
  20.     {
  21.        // This will use app/views/crud/update.thtml        
  22.     }
  23.    
  24.     function delete()
  25.     {
  26.        // This will use app/views/crud/delete.thtml                
  27.     }
  28.    
  29. }

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

Print this Post | Digg This | Stumble It | Delicious

0 Comments

No comments yet...