Thursday, November 12, 2009

CakePHP - Fine-tune Your Scaffolded Application

In my previous post, I tried to explain how you can create a workable application on the fly using scaffolding. Now I'll try to show you how to fine tune your scaffolded application. Since, I'll be referring to my previous post a lot, so, please read that first.


Control Display Fields

In our previous example, say, I want to display only the comments title, body, and created fields. So, I will add the following lines in comments_controller.php (found under '/app/controllers') just below
   var $scaffold;
statement.


function beforeRender() {
if($this->action === 'index'){
$this->set( 'scaffoldFields', array( 'title', 'body', 'created' ) );
  }
}


Note 'beforeRender()' is a CakePHP callback. It gets executed before the actual controller action. This helps you to add some more logic in your controller. There are three controller callback methods in common use: beforeFilter(), beforeRender(), afterFilter()

And here is that much desired screenshot:


For a more detailed discussion, please go through the book.

No comments: