ZF2自定义视图助手没有注册

时间:2015-08-26 11:24:45

标签: php zend-framework zend-framework2

我是ZF2的新用户,正在尝试创建自定义this procedure。在一个名为profiles.phtml的视图中我做了

echo $this->MyModuleHelper()->greetings('stack');

导致

致命错误:Class' Dashboard \ View \ Helper \ MyModuleHelper'在 C:\ dashboard \ Application \ module \ Dashboard \ Module.php 中找不到 112

我错过了什么和/或做错了什么?

应用程序/模块/仪表板/ Module.php

namespace Dashboard;

use Zend\Mvc\ModuleRouteListener;
use Zend\Mvc\MvcEvent;
use Zend\ModuleManager\Feature\ViewHelperProviderInterface;

use Dashboard\View\Helper\MyModuleHelper;

class Module implements ViewHelperProviderInterface {
    public function onBootstrap(MvcEvent $e) {
        //Some stuff 
    }

    public function getConfig() {
        return include __DIR__ . '/config/module.config.php';
    }

    public function getAutoloaderConfig() {
        return array(
            'Zend\Loader\StandardAutoloader' => array(
                'namespaces' => array(
                    __NAMESPACE__ => __DIR__ . '/src/' . __NAMESPACE__,
                ),
            ),
        );
    }

    public function getViewHelperConfig() {
        return array(
            'factories' => array(
                'MyModuleHelper' => function ( $sl ) {
                    return new MyModuleHelper();  //Line 112
                }
            ),
        );  
    }
}

应用程序/模块/仪表板/视图/助手/ MyModuleHelper.php

namespace Dashboard\View\Helper;
use Zend\View\Helper\AbstractHelper;

class MyModuleHelper extends AbstractHelper   {

    public function __invoke() {
        return $this;
    }

    public function greetings( $userName ) {
        return $this->escapeHtml( sprintf("Hello, %s! ", $userName) ); 
    }
}

旁注:我还尝试在module.config.php(而非Module.php)中注册,如

'view_helpers' => array(
    'invokables' => array(
        'MyModuleHelper' => 'Dashboard\View\Helper\MyModuleHelper',
    )
)

1 个答案:

答案 0 :(得分:2)

正如评论中所提到的那样,目录结构搞砸了。

我把我的助手放进去了 Application/module/Dashboard/view/Helper/MyModuleHelper.php

何时应该放入 Application/module/Dashboard/src/Dashboard/View/Helper/MyModuleHelper.php