views:

83

answers:

2

Hi am trying to add custom helper throughout my application

Have done following steps

  1. index.php

    $view = new Zend_View(); $view->addHelperPath('My/View/Helper', 'My_View_Helper'); Zend_Controller_Action_HelperBroker::addHelper($viewRenderer);

  2. Helper class in My/View/Helper

class My_View_Helper_Common extends Zend_View_Helper_Abstract {

public function example()
{
  return "ok"; 
}

}

  1. now calling in view index.phtml

$this->example()

am getting this error

Uncaught exception 'Zend_View_Exception' with message 'script 'error/error.phtml' not found in path (.\application\views\scripts)' in C:\xampp\htdocs\wyfixture\library\Zend\View\Abstract.php:924 Stack trace: #0 C:\xampp\htdocs\wyfixture\library\Zend\View\Abstract.php(827): Zend_View_Abstract->_script('error/error.pht...') #1 C:\xampp\htdocs\wyfixture\library\Zend\Controller\Action\Helper\ViewRenderer.php(903): Zend_View_Abstract->render('error/error.pht...') #2 C:\xampp\htdocs\wyfixture\library\Zend\Controller\Action\Helper\ViewRenderer.php(924): Zend_Controller_Action_Helper_ViewRenderer->renderScript('error/error.pht...', NULL) #3 C:\xampp\htdocs\wyfixture\library\Zend\Controller\Action\Helper\ViewRenderer.php(963): Zend_Controller_Action_Helper_ViewRenderer->render() #4 C:\xampp\htdocs\wyfixture\library\Zend\Controller\Action\HelperBroker.php(277): Zend_Controller_Action_Helper_ViewRenderer->postDispatch() #5 C:\xampp\htdocs\wyfixture\library\Zend\Controller\Action.php(523):

please help me

+2  A: 

Seems like you have two problems here:

  1. Your 'application/views/scripts/error/error.phtml' is missing. You can restore it and you'll get more accurate exception message at once.
  2. Your helper class should contain a method named after the helper.

So, in your case it's file My/View/Helper/Example.php with the following body

class My_View_Helper_Example extends Zend_View_Helper_Abstract {
  public function example() {...}
}

Then you'll be able to call it from the view with

$this->example()
Vika
Thanks Vika ,It's working now.But can we add only one method in a view helper?say if am adding two method like this :class My_View_Helper_Common extends Zend_View_Helper_Abstract{ public function common() { return "ok"; } public function example() { return "ok"; }and if am calling method $this->example(). It's giving same error. but ok for $this->common();Or there is different way to all example in view .Please help me. am newbie to zend and I have to understand all these.
seed_of_tree
+1 for correct answer
Benjamin Cremer
+1  A: 

In addition to Vikas answer.

To call more than one method in a view helper you can use code like this:

In My/View/Helper/Example.php

class My_View_Helper_Example extends Zend_View_Helper_Abstract
{
    public function example()
    {
        return $this;
    }

    public function foo()
    {
        return 'foo';
    }

    public function bar()
    {
        return 'bar';
    }

    public function __toString()
    {
        return $this->foo();
    }
}

In you views:

echo $this->example()->foo() // prints foo
echo $this->example()->bar() // prints bar
echo $this->example() // prints foo
Benjamin Cremer
Thanks Benjamin . It's working now. :)
seed_of_tree
vasim: do not forget to upvote helpful answers. See the FAQ ;)
Benjamin Cremer