0

有没有办法像这样访问在我的模块中注册的自定义视图助手:

'fooBar' => 'Library\FooBar\FooBar'

像这样?

<p>This is partial...</p>
<p><?php echo $this->fooBar(); ?></p>

我可以在视图中使用 fooBar 视图助手,但在部分视图中出现错误。这是我的视图助手:

class FooBar extends AbstractHelper
{

    public function __invoke($module)
    {
        return $this->getView()->myMethod();
    }

}

而且我遇到了一个错误:

Call to a member function myMethod() on a non-object
4

1 回答 1

0

它对我有用。我在 Module.php 文件中配置了类似这样的视图助手。

//module/src/module/View/Helper/Authz.php //查看助手

    use Zend\View\Helper\AbstractHelper;



    class Authz extends AbstractHelper 
    {

    public function isAllowed()
{
}
    }

.

//模块.php

public function bootstrap(Event $e)
    {

    }
    public function onBootstrap($e)
    {

        $app = $e->getApplication();
        $serviceManager = $app->getServiceManager();


        $serviceManager->get('viewhelpermanager')->setFactory('Authz', function ($sm) use ($e)      {
           return new Authz($sm); //view helper
        });


    }

// 部分 // 视图/布局/partials/leftnav.phtml

$userAllow = $this->Authz()->isAllowed(); 

// 在布局中,我在 leftnav.phtml 部分上方调用类似这样的内容。//查看/布局/layout.phtml

<?php echo $this->partial('partials/header', array('company_name' => 'CompanyName')); ?>

// 配置/module.config.php

'view_manager' => array(
        'template_path_stack' => array(
            'modulename' => __DIR__ . '/../view' ,
        ),
        'strategies' => array(
            'ViewJsonStrategy',
        ),

        'display_not_found_reason' => true,
        'display_exceptions'       => true,
        'doctype'                  => 'HTML5',
        'not_found_template'       => 'error/404',
        'exception_template'       => 'error/index',
        'template_map' => array(
            'layout/layout'           => __DIR__ . '/../view/layout/layout.phtml',
            'partials/header'           => __DIR__ . '/../view/layout/partials/header.phtml',
            'partials/leftnav'           => __DIR__ . '/../view/layout/partials/leftnav.phtml',            
            'partials/contents-top'           => __DIR__ . '/../view/layout/partials/contents-top.phtml',            
            'error/404'               => __DIR__ . '/../view/error/404.phtml',
            'error/index'             => __DIR__ . '/../view/error/index.phtml',    
        ),

    ),    
于 2013-03-21T18:56:55.550 回答