Pages

Thursday, August 23, 2012

Zend framework : Writing Custom Action Helpers


We often come across a problem in Zend framework where we need add on demand functionality into any Action Controller. In Zend framework, Action Helpers allows to do the same thing.
Basically Action Helpers helps to inject common Action Controller functionality in to any action with out extending the abstract Action Controller for common functionality.

In below e.g. I have created a custom action helper where I need to get firstname of user based on his userid.

class Zend_Controller_Action_Helper_User extends Zend_Controller_Action_Helper_Abstract
{
    public function firstName( $userid = null ) {
        //fetch from database and return
    }
}

How to call an Action helper in the Action Controller :

$firstName = $this->_helper->user->firstName( 'abc' );

References:
http://framework.zend.com/manual/en/zend.controller.actionhelpers.html
http://framework.zend.com/manual/en/zend.controller.actionhelpers.html#zend.controller.actionhelper.writingyourown

Wednesday, August 22, 2012

Zend framework : Where to write sql queries

Zend Framework provides DbTables i.e for each database table there is an associated DbTable to it. For better understanding please refer to below e.g.

Class Model_DbTable_User extends Zend_Db_Table_Abstract
{
    protected $_name = 'user';

    public function fetchUsers() {

       return $this->select()->from($this->_name)
              ->where('status=?','Active');
   }
}


As you can see from above example, we have to create file named User.php at application/models/DbTable/User.php.

Also note "$_name" where we have to specify the database table name. Here I'm using Zend_Db_Select Object to form SQL queries.

For more information on Db tables and Select Object, please refer to below references:

http://framework.zend.com/manual/en/zend.db.select.html
http://framework.zend.com/manual/en/zend.db.table.html

6D7ZDFF7V2VG

Wednesday, May 30, 2012

Zend Framework : How to redirect to a url

Zend Framework includes several action helpers by default: AutoComplete, ContextSwitch and AjaxContext, FlashMessenger, Json and Redirector.

Redirector action helper provides different implementations for redirecting to internal and external pages from your application.

Here is an example below to explain to redirect to URL from controller:

This will perform a redirect followed by an exit

$this->_helper->redirector->gotoSimpleAndExit(action_name,controller_name,module_name, $params);


For e.g if you want to redirect to abc action of foo controller of baz module (e.g. URL : /baz/foo/abc )

$this->_helper->redirector->gotoSimpleAndExit('abc','foo','baz');

$this->_helper->redirector->gotoUrlAndExit('/baz/foo/abc');


If you want to use redirect multiple times in a controller in different actions,
then you can create an Redirector object in the init method of the controller as show below:

class SomeController extends Zend_Controller_Action
{
    protected $_redirector = null;

    public function init()
    {
        $this->_redirector = $this->_helper->getHelper('Redirector');
    }

    public function myAction()
    {
        $this->_redirector->gotoUrlAndExit('/baz/foo/abc');
    }
}