I am having some trouble porting an site from zf2 to zf3. I have added a single module to the skeleton app and built it from there, but keep on getting the following exception when a custom view helper is called in my layout:
Fatal error: Uncaught Error: Class 'Application\View\Helper\MyHelper' not found in C:\workspace\my-project\vendor\zendframework\zend-servicemanager\src\Factory\InvokableFactory.php:30
Commenting out the call to this view helper simply causes another error later:
Fatal error: Method Zend\View\Helper\Navigation\Menu::__toString() must not throw an exception, caught Error: Class 'Zend\View\Helper\Translate' not found in C:\workspace\my-project\module\Application\view\layout\site.phtml on line 0
..suggesting a general problem finding view helpers. Here are my configs:
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,
],
];
So far, all the basic skeleton app config. Now for my module config, where the custom view helper is registered:
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'
)
),
);
And finally, the custom view helper itself:
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';
}
}
...which all seems to be straightforward, but i can't see why the view helper is not being found?