I'm new to ZF2 and are trying to create a custom view helper. In a view called profiles.phtml I do
echo $this->MyModuleHelper()->greetings('stack');
Which is resulting in
Fatal error: Class 'Dashboard\View\Helper\MyModuleHelper' not found in C:\dashboard\Application\module\Dashboard\Module.php on line 112
What am I missing and/or doing wrong?
Application/module/Dashboard/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
}
),
);
}
}
Application/module/Dashboard/view/Helper/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) );
}
}
Side note: I've also tried registering it in module.config.php (instead of Module.php) like
'view_helpers' => array(
'invokables' => array(
'MyModuleHelper' => 'Dashboard\View\Helper\MyModuleHelper',
)
)
module.config.php? Any errors etc? I recommend this approach over putting it inModule.phpif it's an invokable class. Also check obvious stuff like is yourmodule.config.phpbeing cached? - Ankhmodule.config.phpI get Zend\View\HelperPluginManager::createFromInvokable: failed retrieving "mymodulehelper(alias: MyModuleHelper)" via invokable class "Dashboard\View\Helper\MyModuleHelper"; class does not exist. The reason why I'm going for the first approiach is that I am going to access the service manager and pass a database object to MyModuleHelper. - dakerApplication/module/Dashboard/view/Helper/MyModuleHelper.phpThat doesn't look right, it should beApplication/module/Dashboard/src/Dashboard/View/Helper/MyModuleHelper.php- Ankh