Code snippets for symfony 1.x

Navigation

Snippets tagged "action helper"

use helpers from an action

technically this breaks MVC separation. but it can be handy (for formating data being stored to a database from an action, for example. This is most easily accomplished by copying the use_helper code to a custom class.

create a custom class in project\lib (or app\lib). i'll use Misc.class.php.

<?php
 
class Misc
{
    public static function use_helper($helperName)
    {
      static $loaded = array();
      if (isset($loaded[$helperName]))
      {
        return;
      }
 
      if (is_readable(sfConfig::get('sf_symfony_lib_dir').'/helper/'.$helperName.'Helper.php'))
      {
        // global helper
        include_once(sfConfig::get('sf_symfony_lib_dir').'/helper/'.$helperName.'Helper.php');
      }
      else if (is_readable(sfConfig::get('sf_app_module_dir').'/'.sfContext::getInstance()->getModuleName().'/'.sfConfig::get('sf_app_module_lib_dir_name').'/helper/'.$helperName.'Helper.php'))
      {
        // current module helper
        include_once(sfConfig::get('sf_app_module_dir').'/'.sfContext::getInstance()->getModuleName().'/'.sfConfig::get('sf_app_module_lib_dir_name').'/helper/'.$helperName.'Helper.php');
      }
      else
      {
        // helper in include_path
        include_once('helper/'.$helperName.'Helper.php');
      }
      $loaded[$helperName] = true;
    }
}

(clear your cache after adding a custom class, of course).

now to use a helper from an action:

Misc::use_helper('Date');
....
by hansbricks on 2006-08-02, tagged action  helper 
(4 comments)