To refer any php function, php.net is the best site but the same is not for Zend, The Zend Documentation is so poor. The best developers those who have given their best to the Zend Framework, but have not given enough to Zend Documentation.

Let’s move to the main topic. Today after a long time I am goin to Show you that how would you create “Zend Controller Action Helper”

Don’t get confuse between the Zend_View_Helper and Zend_Controller_Action_Helper. In one term both are the helpers but for different section as their name suggests. The first one is for View Helper and another is for the Zend Action controller Helper.

1. Zend View Helper:

Creating A Helper Class:

I have created the helper class named Myapp_View_Helper_Adminhelper the exist in the folder Myapp/view/helper/Adminhelper.php

<?php
class Myapp_View_Helper_Adminhelper{

public function adminhelper(){
return $this;
}

public function formatDate($date){
return date(‘d-m-Y h:i:s a’,strtotime($date));
}
}

The View Helper class must have function name same as the name of the  class name in small letters, in short kind of constructor that is not called using __construct() . So you may wonder that what if one want to create more than one helpers function in the same class. So I made a trick here is that I have returned the class object in the main function and so now I can call as many functions as I want defined in the helper class.

 

Class has been created properly but to tell Zend Please dont trow any errors when I call this View Helper class functions in view script, we have to add this path into view helpers path. If you have created _initView() function in your bootstrap.php modify it as below.

protected function _initView(){
// Initialize view
$view = new Zend_View();
$view->doctype(‘XHTML1_STRICT’);

// Add it to the ViewRenderer
$viewRenderer = Zend_Controller_Action_HelperBroker::getStaticHelper(‘ViewRenderer’);
$viewRenderer->setView($view);

// Adding View Helper Path
$view->addHelperPath(APPLICATION_PATH.’/views/helpers’, ‘Myapp_View_Helper’);

// Return it, so that it can be stored by the bootstrap
return $view;
}

If you dont have _initView() function in your bootstrap.php filen then just create any function that you like (e.g. _initViewHelpers)and inside that funciton the $view object as above and then add code mentioning adding the the View Helper path. That’s it. You can verify that view helpers path is included properly or not by using $view->getHelpersPath() function, which will return all helpers path.

Now we can call view helpers class function as…

<?php echo $this->adminhelper()->formatDate($requestedps->created_on); ?>

In above code $this->adminhelper()  return the object of the same class and hence you can call function formatDate() like as above.

This is what you just need to implement for view helpers. Zend has by default given some view helpers default that you can use it with out any hassle.

Now Lets Continue with action helpers:

2. Zend Controller Action Helper:

I was working in the admin panel coding. In the controller actions, in all the pages that was displaying the all records form the database table I was using zend pagination plugin. But I had to repeat small pagination code snippet in every action. I could have created one normal function in the controller class but this would be limited to that controller only and I wanted pagination to use in Front end also. It was the perfect time to start with Zend Controller Action.

I created one directory named actionhelpers inside the application folder, in short at same level where my views, controllers are. you can give any name what ever you like or you can put folder inside folder. The thing that matter is that as we had registered zend view helpers as same way we have to register Action Helpers and at that time you have to take care while registering it in you bootstrap.php file

<?php
class Myapp_Actionhelper_Common extends Zend_Controller_Action_Helper_Abstract{

protected $view;
public function pagination($result_set, $style = ‘digg’){

$page = $this->getRequest()->getParam(‘page’,1);
$records_per_page = $this->getRequest()->getParam(‘shown’,10);

$paginator = Zend_Paginator::factory($result_set);
$paginator->setItemCountPerPage($records_per_page);
$paginator->setCurrentPageNumber($page);

$record_count = sizeof($result_set);

$view = $this->getView();
$view->page = $page;
$view->records_per_page = $records_per_page;
$view->numrows = $record_count;
$view->pagination_config = array(‘total_items’=>$record_count,’items_per_page’=>$records_per_page,’style’=>$style);
$view->records = $paginator;
}

public function getView(){

if(null !== $this->view){
return $this->view;
}

$controller = $this->getActionController();
$this->view = $controller->view;
return $this->view;
}
}

Now first you will have to tell zend where your action helper it so it doesn’t throw an error

public function _initActionHelpers(){

// Adding Action Helper Path
Zend_Controller_Action_HelperBroker::addPath(APPLICATION_PATH.’/actionhelpers’, ‘Myapp_Actionhelper’);
}

Here “APPLICATION_PATH” is the constant value and is the path string to my application folder.

Now in your controller action, you can call this function as

public someAction(){

$this->_helper->Common->pagination($request_res);

}

where $request_res is the record set the i fetch from database table.

 

 

Advertisements