views:

264

answers:

5

I wrote few custom view helpers but I have a little trouble using them. If I add the helper path in controller action like this:

public function fooAction()
{
    $this->view->addHelperPath('My/View/Helper', 'My_View_Helper');
}

Then I can use the views from that path without a problem. But when I add the path in the bootstrap file like this:

protected function _initView()
{
    $this->view = new Zend_View();
    $this->view->doctype('XHTML1_STRICT');
    $this->view->headScript()->appendFile($this->view->baseUrl()
                                          . '/js/jquery-ui/jquery.js');
    $this->view->headMeta()->appendHttpEquiv('Content-Type',
                                       'text/html; charset=UTF-8');
    $this->view->headMeta()->appendHttpEquiv('Content-Style-Type',
                                             'text/css');
    $this->view->headMeta()->appendHttpEquiv('Content-Language', 'sk');
    $this->view->headLink()->appendStylesheet($this->view->baseUrl()
                                              . '/css/reset.css');
    $this->view->addHelperPath('My/View/Helper', 'My_View_Helper');
}

Then the view helpers don't work. Why is that? It's too troublesome to add the path in every controller action. Here is an example of how my custom view helpers look:

class My_View_Helper_FooBar
{
    public function fooBar() {
        return 'hello world';
    }
}

I use them like this in views:

<?php echo $this->fooBar(); ?>

Should I post my whole bootstrap file?

UPDATE:

Added complete bootstrap file just in case:

class Bootstrap extends Zend_Application_Bootstrap_Bootstrap
{
    protected function _initFrontController()
    {
        $this->frontController = Zend_Controller_Front::getInstance();
        $this->frontController->addModuleDirectory(APPLICATION_PATH
                                                   . '/modules');
        Zend_Controller_Action_HelperBroker::addPath(
            'My/Controller/Action/Helper',
            'My_Controller_Action_Helper'
        );
        $this->frontController->registerPlugin(new My_Controller_Plugin_Auth());
        $this->frontController->setBaseUrl('/');
    }

    protected function _initView()
    {
        $this->view = new Zend_View();
        $this->view->doctype('XHTML1_STRICT');
        $this->view->headScript()->appendFile($this->view->baseUrl()
                                              . '/js/jquery-ui/jquery.js');
        $this->view->headMeta()->appendHttpEquiv('Content-Type',
                                           'text/html; charset=UTF-8');
        $this->view->headMeta()->appendHttpEquiv('Content-Style-Type',
                                                 'text/css');
        $this->view->headMeta()->appendHttpEquiv('Content-Language', 'sk');
        $this->view->headLink()->appendStylesheet($this->view->baseUrl()
                                                  . '/css/reset.css');
        $this->view->addHelperPath('My/View/Helper', 'My_View_Helper');
    }

    protected function _initDb()
    {
        $this->configuration = new Zend_Config_Ini(APPLICATION_PATH
                                                   . '/configs/application.ini',
                                                   APPLICATION_ENVIRONMENT);
        $this->dbAdapter = Zend_Db::factory($this->configuration->database);
        Zend_Db_Table_Abstract::setDefaultAdapter($this->dbAdapter);
        $stmt = new Zend_Db_Statement_Pdo($this->dbAdapter,
                                          "SET NAMES 'utf8'");
        $stmt->execute();
    }

    protected function _initAuth()
    {
        $this->auth = Zend_Auth::getInstance();
    }

    protected function _initCache()
    {
        $frontend= array('lifetime' => 7200,
                         'automatic_serialization' => true);
        $backend= array('cache_dir' => 'cache');
        $this->cache = Zend_Cache::factory('core',
                                           'File',
                                           $frontend,
                                           $backend);
    }

    public function _initTranslate()
    {
        $this->translate = new Zend_Translate('Array',
                                              BASE_PATH . '/languages/Slovak.php',
                                              'sk_SK');
        $this->translate->setLocale('sk_SK');
    }

    protected function _initRegistry()
    {
        $this->registry = Zend_Registry::getInstance();
        $this->registry->configuration = $this->configuration;
        $this->registry->dbAdapter = $this->dbAdapter;
        $this->registry->auth = $this->auth;
        $this->registry->cache = $this->cache;
        $this->registry->Zend_Translate = $this->translate;
    }

    protected function _initUnset()
    {
        unset($this->frontController,
              $this->view,
              $this->configuration,
              $this->dbAdapter,
              $this->auth,
              $this->cache,
              $this->translate,
              $this->registry);
    }

    protected function _initGetRidOfMagicQuotes()
    {
        if (get_magic_quotes_gpc()) {
            function stripslashes_deep($value) {
                $value = is_array($value) ?
                         array_map('stripslashes_deep', $value) :
                         stripslashes($value);
                return $value;
            }

            $_POST = array_map('stripslashes_deep', $_POST);
            $_GET = array_map('stripslashes_deep', $_GET);
            $_COOKIE = array_map('stripslashes_deep', $_COOKIE);
            $_REQUEST = array_map('stripslashes_deep', $_REQUEST);
        }
    }

    public function run()
    {
        $frontController = Zend_Controller_Front::getInstance();
        $frontController->dispatch();
    }
}
+1  A: 

I in my _initView() have something like this:

 protected function _initView() {

    $view =  new Zend_View();

    #some view initialization ...

    $view->addHelperPath(APPLICATION_PATH . '/views/helpers', 'My_View_Helper');

    return $view;
}

Then in a view I can execute:

<?php echo $this->fooBar(); ?>

Without APPLICATION_PATH it does not work in my case.

Marcin
+1  A: 

Just a thought: are you sure that the view that you are creating in your bootstrap ($this->view = new Zend_View();) is the same as '$this' in your view file?

I think you would need to change your initView code to the following:

protected function _initView()
{
  $view = new Zend_View();
  $view->doctype('XHTML1_STRICT');
  $view->headScript()->appendFile($this->view->baseUrl()
                                      . '/js/jquery-ui/jquery.js');
  $view->headMeta()->appendHttpEquiv('Content-Type',
                                   'text/html; charset=UTF-8');
  $view->headMeta()->appendHttpEquiv('Content-Style-Type',
                                         'text/css');
  $view->headMeta()->appendHttpEquiv('Content-Language', 'sk');
  $view->headLink()->appendStylesheet($this->view->baseUrl()
                                          . '/css/reset.css');
  $view->addHelperPath('My/View/Helper', 'My_View_Helper');

  return $view;
}

If you have some View related settings in your config.ini file, you might want to change your code a little bit:

protected function _initMyView()
{
   $view = $this->bootstrap('view')->getResource('view');
   ...

instead of:

protected function _initView()
{
  $view = new Zend_View();
  ....
Kenji Baheux
As Marchin said, you probably also need to use APPLICATION_PATH
Kenji Baheux
+4  A: 

Solved. I just needed to add these lines at the end of _initView() method:

$viewRenderer = Zend_Controller_Action_HelperBroker::getStaticHelper('ViewRenderer'); 
$viewRenderer->setView($this->view); 
Richard Knop
So that was the missing link... Thanks for sharing!
Kenji Baheux
+1  A: 

You might consider adding another init function just for your view helpers:

protected function _initViewHelpers()
{
     $this->bootstrap('view');
     $view = $this->getResource('view');

     $view->addHelperPath('My/View/Helper', 'My_View_Helper');
}

This way the built in view setup is not overridden.

smack0007
A: 

If you add only $this->view->addHelperPath('My/View/Helper', 'My_View_Helper'); in your bootstrap use this format:
class Zend_View_Helper_FooBar extends Zend_View_Helper_Abstract
{
public function fooBar() {
return 'hello world';
}
}

Behrang