4
votes

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',
    )
)
1
What happened when you tried registering it in module.config.php? Any errors etc? I recommend this approach over putting it in Module.php if it's an invokable class. Also check obvious stuff like is your module.config.php being cached? - Ankh
@Svengali when doing it from module.config.php I 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. - daker
That's fine, if it needs the service manager then that's fair enough :) That error makes it sound like it's a trivial problem we're dealing with, however it might be tricky to find. Triple check that your namespaces, class and file names are all correct. Your actual view helper looks fine to me. - Ankh
Application/module/Dashboard/view/Helper/MyModuleHelper.php That doesn't look right, it should be Application/module/Dashboard/src/Dashboard/View/Helper/MyModuleHelper.php - Ankh
Looks like your directory structure is wrong. Please show it. Also, it's good to move the conversation in chat. - Stanimir Dimitrov

1 Answers

2
votes

As mentioned in the comments I had the directory structure messed up.

I placed my helper in Application/module/Dashboard/view/Helper/MyModuleHelper.php

When it should have been put in Application/module/Dashboard/src/Dashboard/View/Helper/MyModuleHelper.php