Reduce duplicated code in Zend_Controller_Action

In most of the Zend Framework projects that i worked on, i had some common actions such as, listAction, newAction, and editAction. Each of these actions have similar structure and code lines. The differences are in the name of the model or the form or the messages on a successful submission and errors.

listAction
Show a table view of a collection of entities (products or customers) with functinalities like pagination, sorting, and filtering.
newAction
Form for creating a new entity. Such as, a form for creating a new product.
editAction
Form for editing an existing entity. Such as a form for editting an existing product. The product Id sepeified in the url parameter with a name of “id”.

I wanted to remove all of the duplicated code and reduce the time I spend on copying and pasting the same code again and again. So I came up with the following action helper to do that.

Class can be found here.

How to use it?

To view a list of products

$options = array(
'title' => 'List Products',
'model' => array(
'className' => 'products'
),
);
$this->_helper->action($options)->listAction();

To create a new product

$options = array(
'title' => 'Create a new product',
'model' => array(
'className' => 'products'
),
'form' => array(
'className' => 'createProduct'
),
'message' => array(
'success' => 'Product created.',
),
'goto' => array(
'save'  => 'admin/products',
'apply' => 'admin/products/edit/id/{id}'
)
);
$this->_helper->action($options)->formNewAction();

To edit a product

$options = array(
'title' => 'Edit a Product ({title})',
'model' => array(
'className' => 'products'
),
'form' => array(
'className' => 'createProduct'
),
'message' => array(
'success' => 'Product created.',
'error'   => array(
'Product Id is invalid.',
'Unknown product.'
)
),
'goto' => array(
'save'  => 'admin/products',
'apply' => 'admin/products/edit/id/{id}'
)
);
$this->_helper->action($options)->formNewAction();

An ajax request

1. in your controller create a public method.

public function productPriceAjax() {
    // your code here
    $model = new Model_Product();
    $price = $model->getPrice();
    $output = array(
         'price' => $price,
    );
    echo Zend_Json::encode($output);
    return false;
}

2. in any controller action you can add the following to convert it into ajax. it will automatically disable the layout the render view.

$this->_setParam('task', 'productPrice');
$this->_helper->action()->ajaxAction();

Any comments?

Leave A Reply

Comments

Jim (Feb 11, 2010)

Mohammed

I really like the approach you’ve taken. can you provide more of the calling context of this core controller? Or perhaps a small working example from front end through this controller? It would really help apply these concepts, and perhaps some of the ideas of your controller to my current project. Thanks, in advance.

mohammed (Feb 11, 2010)

Hi Jim

i did not understand what you are after. There are 3 working examples in the post.

Jim (Feb 12, 2010)

I am looking for the use case specifically for calling via ajax.

mohammed (Feb 12, 2010)

I have updated the post with example on how to use the ajax calling.

Tags