Zend framework zend,我不能给任何视图助手打电话

Zend framework zend,我不能给任何视图助手打电话,zend-framework,Zend Framework,我的视图文件夹中有一个helpers文件夹,其中有一个名为Log.php的助手 /views/helpers/log.php 其中包括: class Zend_View_Helper_Log extends Zend_View_Helper_Abstract { public function loggedAs () { $auth = Zend_Auth::getInstance(); if ($auth->hasIdentity())

我的视图文件夹中有一个helpers文件夹,其中有一个名为Log.php的助手 /views/helpers/log.php

其中包括:

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>';
        } 


    }
}
class Zend\u View\u Helper\u Log扩展了Zend\u View\u Helper\u抽象
{
公共功能日志()
{
$auth=Zend_auth::getInstance();
如果($auth->hasintity()){
$username=$auth->getIdentity()->uname;
$logoutUrl=$this->view->url(数组('controller'=>'auth','action'=>'logout'),null,true);
返回“Hello”。$username.';
} 
}
}
我如何从布局中调用它?还是风景?我试过$this->_helpers->log->loggedAs()


但是没有显示任何内容,只显示一个错误:致命错误:对…中的非对象调用成员函数loggedAs()。

您的helper类应该有一个与helper名称匹配的方法,这就是您所调用的方法。因此,如果您想从模板中调用
loggedAs()
,那么您应该将助手命名为:

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>';
        } 
    }
}

我还建议在类名中使用您自己的名称空间,而不是Zend,但您所使用的方法也应该适用。

我对ZF有一点经验。昨天我遇到了同样的问题,我决定使用以下代码。 在main Bootstrap.php中,我定义了助手路径和前缀

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");
}
在查看文件之后,我使用了下一个语法

$this->getPhoneString($value['per_telephone_number']);
其中,我的助手类中的getPhoneString方法my\u Helper\u View\u getPhoneString


希望这个例子对你有用:)

尝试过这个,我得到:在注册表中找不到名为“LoggedAs”的插件;使用的路径:Zend_View_Helper
$this->getPhoneString($value['per_telephone_number']);