将站点从zf2移植到zf3时遇到麻烦。我已经向骨架应用程序添加了一个模块,并从那里构建了它,但是当在布局中调用自定义视图助手时,继续出现以下异常:
致命错误:未捕获错误:在C:\ workspace \ my-project \ vendor \ zendframework \ zend-servicemanager \ src \ Factory \ InvokableFactory.php:30中找不到类'Application \ View \ Helper \ MyHelper' >
注释此视图帮助器的调用只会在稍后导致另一个错误:
致命错误:方法Zend \ View \ Helper \ Navigation \ Menu :: __ toString()不得引发异常,并捕获错误:在C:\ workspace \ my-中找不到类'Zend \ View \ Helper \ Translate'第0行上的project \ 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';
}
}
...这似乎都很简单,但是我看不到为什么找不到视图助手?