我在将网站从zf2移植到zf3时遇到了一些麻烦 . 我已经在骨架应用程序中添加了一个模块并从那里构建了它,但在我的布局中调用自定义视图助手时,继续获得以下异常:

致命错误:未捕获错误:在C:\ workspace \ my-project \ vendor \ zendframework \ zend-servicemanager \ src \ Factory \ InvokableFactory.php中找不到类'Application \ View \ Helper \ MyHelper':30

注释掉对此视图助手的调用只会导致以后出现另一个错误:

致命错误:方法Zend \ View \ Helper \ Navigation \ Menu :: __ toString()不得抛出异常,捕获错误:在C:\ workspace \ my-project \中找不到类'Zend \ View \ Helper \ Translate'第0行的module \ Application \ view \ layout \ site.phtml

..提出查找助手的一般问题 . 这是我的配置:

my-project/config/modules.config.php

return [
    'Zend\Mail',
    'Zend\Paginator',
    'Zend\ServiceManager\Di',
    'Zend\Session',
    'Zend\I18n',
    'Zend\Mvc\Plugin\Prg',
    'Zend\Mvc\Plugin\Identity',
    'Zend\Mvc\Plugin\FlashMessenger',
    'Zend\Mvc\Plugin\FilePrg',
    'Zend\Mvc\I18n',
    'Zend\Mvc\Console',
    'Zend\Navigation',
    'Zend\Log',
    'Zend\Form',
    'Zend\Db',
    'Zend\Cache',
    'Zend\Router',
    'Zend\Validator',
    'Application',  
];

my-project/config/application.config.php

return [
    'modules' => require __DIR__ . '/modules.config.php',
    'module_listener_options' => [
        'module_paths' => [
            './module',
            './vendor',
        ],

        'config_glob_paths' => [
            realpath(__DIR__) . '/autoload/{{,*.}global,{,*.}local}.php',
        ],

         'config_cache_enabled' => false,
        'config_cache_key' => 'application.config.cache',
        'module_map_cache_enabled' => false,
        'module_map_cache_key' => 'application.module.cache',
        'cache_dir' => 'data/cache/',
        // 'check_dependencies' => true,
    ],
];

my-project/config/development.config.php

return [
// Additional modules to include when in development mode
'modules' => [
    'ZendDeveloperTools',
],
// Configuration overrides during development mode
'module_listener_options' => [
    'config_glob_paths' => [realpath(__DIR__) . '/autoload/{,*.}{global,local}-development.php'],
    'config_cache_enabled' => false,
    'module_map_cache_enabled' => false,
    ],
];

到目前为止,所有基本的骨架应用程序配置 . 现在为我的模块配置,注册自定义视图助手:

module/Application/config/module.config.php

<?php

namespace Application;

use Zend\ServiceManager\Factory\InvokableFactory;
use Zend\Navigation\Service\NavigationAbstractServiceFactory;

use Application\View\Helper\MyHelper;

return array(
    'controllers' => [
        'factories' => [
             Controller\IndexController::class => InvokableFactory::class,
        ],
    ],
    'view_manager' => [
        'display_not_found_reason' => true,
        'display_exceptions'       => true,
        'doctype'                  => 'HTML5',
        'not_found_template'       => 'error/404',
        'exception_template'       => 'error/index',
        //'template_map' => include __DIR__ . '/template_map.config.php',
        'template_map' => [
                'layout/layout'           => __DIR__ . '/../view/layout/site.phtml',
                'application/index/index' => __DIR__ . '/../view/application/index/index.phtml',
                'error/404'               => __DIR__ . '/../view/error/404.phtml',
                'error/index'             => __DIR__ . '/../view/error/index.phtml',
        ],
        'template_path_stack' => [
              __DIR__ . '/../view',
        ],
    ],


    'translator' => array(
        'locale' => 'en_US',
        'translation_file_patterns' => array(
            array(
            'type' => 'gettext',
            'base_dir' => __DIR__ . '/data/language',
            'pattern' => '%s.mo'
            )
        )
    ),

    'view_helpers' => [
        'aliases' => [
                'myHelper' => MyHelper::class,
        ],
        'factories' => [
                MyHelper::class => InvokableFactory::class,
        ],
    ],

    /**
     * In order to use multiple navigations, we need to the use the abstract factory.
     */
    'service_manager' => array(
        'abstract_factories' => array(
            NavigationAbstractServiceFactory::class,
        ),
        'factories' => array(
            'translator' => 'Zend\I18n\Translator\TranslatorServiceFactory'
        )
    ),
);

最后,自定义视图助手本身:

my-project/module/Application/src/Application/View/Helper/MyHelper.php

<?php

namespace Application\View\Helper;

use Zend\View\Helper\AbstractHelper;

class MyHelper extends AbstractHelper
{
        public function __invoke(){
            return 'foo';
        }
}

...这一切似乎都很简单,但我不明白为什么没有找到视图助手?