dean jase
dean jase

Reputation: 1161

zend, i cant call any view helpers

i have a got a helpers folder in my views folder with a helper called Log.php /views/helpers/log.php

which contains:

class Zend_View_Helper_Log extends Zend_View_Helper_Abstract 
{
    public function loggedAs ()
    {
        $auth = Zend_Auth::getInstance();
        if ($auth->hasIdentity()) {
            $username = $auth->getIdentity()->uname;
            $logoutUrl = $this->view->url(array('controller'=>'auth', 'action'=>'logout'), null, true);
            return 'Hello' . $username .  '. <a href="'.$logouturl.'">Logout?</a>';
        } 


    }
}

how can i call this from layouts? or views? i tried $this->_helpers->log->loggedAs();

but doesnt display anything, just an error:Fatal error: Call to a member function loggedAs() on a non-object in ...

Upvotes: 0

Views: 499

Answers (2)

Andryi Ilnytskyi
Andryi Ilnytskyi

Reputation: 36

I have a little experience in ZF. Yesterday I have the same problem and I decided its with the following code. In the main Bootstrap.php I defined helper Path and Prefix

protected function _initDoctype()
{
    $this->bootstrap('view');
    $view = $this->getResource('view');
    $view->doctype('XHTML1_STRICT');

    $view->addHelperPath(APPLICATION_PATH . "/../library/My/Helper/View", "My_Helper_View");
}

After that in view file I used next syntax

$this->getPhoneString($value['per_telephone_number']);

where getPhoneString method in my Helper Class My_Helper_View_GetPhoneString

Hope this example will be useful for you :)

Upvotes: 1

Tim Fountain
Tim Fountain

Reputation: 33148

Your helper class should have a method that matches the name of the helper, and this is what you call. So if you want to call loggedAs() from your templates then this is what you should name your helper:

class Zend_View_Helper_LoggedAs extends Zend_View_Helper_Abstract 
{
    public function loggedAs()
    {
        $auth = Zend_Auth::getInstance();
        if ($auth->hasIdentity()) {
            $username = $auth->getIdentity()->uname;
            $logoutUrl = $this->view->url(array('controller'=>'auth', 'action'=>'logout'), null, true);
            return 'Hello' . $username .  '. <a href="'.$logouturl.'">Logout?</a>';
        } 
    }
}

this should then live in a file at application/views/helpers/LoggedAs.php, and you'd call it from within your templates like this:

<?=$this->loggedAs()?>

I'd also recommend using your own namespace instead of Zend in the class name, but the way you've done it should work as well.

Upvotes: 0

Related Questions