Creating Your Own Helper – Zend Framework


In Zend Framework, most of the important functions are built into the framework. Most of them are in the View Helpers or Action Helper. But in big projects, you will always need to inject some functionality that are too small to create a class/plugin.

You can always create a protected function inside a controller but what if that function is need in more than one controller? Creating a base controller that you will extend later will only create troubles when controllers don’t initialize properly or if not all controllers need those functionalities.

So, here is how to create your own helper:

1. Create the helper directory structure.

Your helper class name must be compatible with Zend’s naming conventions to allow Zend_Loader to load it automatically. Your helper must be placed on a directory that is set as include path.

In my case I have only on included path, and that is the library directory, so I placed them there:

library/Zend/[Zend Library]
library/Dc/Helper/[My Helpers] – this is where my helpers located

2. Tell Zend Framework the location of your Helpers

So that Zend Framework includes your helpers, add this line to your bootstrap:

  1. /**
  2. * Setup the Custom Helpers
  3. */
  4. Zend_Controller_Action_HelperBroker::addPrefix(‘Dc_Helper’);
	/**
	 * Setup the Custom Helpers
	 */
	Zend_Controller_Action_HelperBroker::addPrefix('Dc_Helper');

That way, the folder Dc/Helper/ is read when a helper is not found on its default location inside Zend’s directory.

3. Create the Helper

Now, it’s time to create the actual helper. To create the helper, you must extend Zend_Controller_Action_Helper_Abstract and do some initialization on the constructor.

The helper below is called DaysInMonth, which returns the number of days in a certain month where month and year is given.

  1. /**
  2. * Action Helper for loading forms
  3. *
  4. * @uses Zend_Controller_Action_Helper_Abstract
  5. */
  6. class Dc_Helper_DaysInMonth extends Zend_Controller_Action_Helper_Abstract
  7. {
  8. /**
  9. * First entry is for January
  10. */
  11. protected $daysInMonth = array(31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);
  12. /**
  13. * @var Zend_Loader_PluginLoader
  14. */
  15. public $pluginLoader;
  16. /**
  17. * Constructor: initialize plugin loader
  18. *
  19. * @return void
  20. */
  21. public function __construct()
  22. {
  23. $this->pluginLoader = new Zend_Loader_PluginLoader();
  24. }
  25. public function getDaysInMonth($month, $year)
  26. {
  27. if ($month < 1 || $month > 12)
  28. {
  29. return 0;
  30. }
  31. $d = $this->daysInMonth[$month – 1];
  32. if ($month == 2)
  33. {
  34. // Check for leap year
  35. // Forget the 4000 rule, I doubt I’ll be around then…
  36. if ($year%4 == 0)
  37. {
  38. if ($year%100 == 0)
  39. {
  40. if ($year%400 == 0)
  41. {
  42. $d = 29;
  43. }
  44. }
  45. else
  46. {
  47. $d = 29;
  48. }
  49. }
  50. }
  51. return $d;
  52. }
  53. /**
  54. * Strategy pattern: call helper as broker method
  55. *
  56. * @param  int $month
  57. * @param  int $year
  58. * @return int
  59. */
  60. public function direct($month, $year)
  61. {
  62. return $this->getDaysInMonth($month, $year);
  63. }
  64. }
/**
 * Action Helper for loading forms
 *
 * @uses Zend_Controller_Action_Helper_Abstract
 */
class Dc_Helper_DaysInMonth extends Zend_Controller_Action_Helper_Abstract
{
	/**
	 * First entry is for January
	 */
	protected $daysInMonth = array(31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);
    /**
     * @var Zend_Loader_PluginLoader
     */
    public $pluginLoader;

    /**
     * Constructor: initialize plugin loader
     *
     * @return void
     */
    public function __construct()
    {
        $this->pluginLoader = new Zend_Loader_PluginLoader();
    }

    public function getDaysInMonth($month, $year)
    {
        if ($month < 1 || $month > 12)
        {
            return 0;
        }

        $d = $this->daysInMonth[$month - 1];

        if ($month == 2)
        {
            // Check for leap year
            // Forget the 4000 rule, I doubt I'll be around then...

            if ($year%4 == 0)
            {
                if ($year%100 == 0)
                {
                    if ($year%400 == 0)
                    {
                        $d = 29;
                    }
                }
                else
                {
                    $d = 29;
                }
            }
        }

        return $d;
    }

    /**
     * Strategy pattern: call helper as broker method
     *
     * @param  int $month
     * @param  int $year
     * @return int
     */
    public function direct($month, $year)
    {
        return $this->getDaysInMonth($month, $year);
    }
}

The public function direct is the entry point of your helper. It just accepts the parameters then call the function to get the days in a month. That is called Strategy Pattern.

You’ll notice that what I was really trying to create was just the function getGaysInMonth.

4. Call the helper inside your controller

To call my newly created helper, here it is (inside the controller)

  1. $days = $this->_helper->daysInMonth(4, 2009);
$days = $this->_helper->daysInMonth(4, 2009);

Source: http://devzone.zend.com/article/3350-Action-Helpers-in-Zend-Framework

Note: As you’ve seen, it is complicated and bloated rather than elegant to create such helpers isn’t it? That is the bad side but also the good side of Zend Framework.

blog.lysender.com

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s