zf2跨模块查看助手

I created a helper in the application module, and it works perfectly. When I try to load it from another modules, such as user, it tells me that it can not find the class.

Class 'Application \ View \ Helper \ Footertable' not found

I tried to put this code in module.config.php well as the application module even in the same file of the user module.

'view_helpers' => array (
    'invokables' => array (
    'footertable' => 'Application\View\Helper\Footertable' 
    ) 
), 

I think it's a problem autoloading class but I can not find information on how to load this helper when you are in another module

I invoke helper in view file using

$this->footertable()

work perfectly in application module but not in user module

any idea?

Hello, but my code is correct

<?php

namespace Application\View\Helper;

use Zend\View\Helper\AbstractHelper;

class Footertable extends AbstractHelper{
    protected  $inizioFine;
    protected  $numero;

    public function __invoke($inizioFine,$numero){
        $this->inizioFine = $inizioFine;
        $this->numero = $numero;
        echo  sprintf('Mostra %d a %d di %d record',$this->inizioFine['start'],$this->inizioFine['end'],$this->numero);
    }
}

the space in config is an copy & past errors. I still have the same problem: can't load helper from another module

update full error

Fatal error: Class 'Application\View\Helper\Footertable' not found in D:\www\httpdocs\test\vendor\zendframework\zendframework\library\Zend\ServiceManager\AbstractPluginManager.php on line 170

path is

D:\www\httpdocs\test\module\Application\src\View\Helper\Footertable.php

The path you posted doesn't look right. All the files in src for your Application module should be inside a folder called Application, since that's your top level namespace. So the path should be:

D:\www\httpdocs\test\module\Application\src\Application\View\Helper\Footertable.php

That would explain why the helper can't be autoloaded, but I don't understand how it works in the application module if this is the case.

Your configuration seems good. Probably the problem is in your helper class signature.

Since PhpRenderer composes a HelperPluginManager instance to manage helpers, your helper should implement the HelperInterface (1) to be registered correctly. Also you should write an __invoke() method within your helper to invoke it by method call. (2)

So, in your Footertable class, simply extend the AbstractHelper and make sure you have an _invoke() method. This is recommended way to write custom view helpers in documentation.

For example:

<?php

namespace Application\View\Helper;

use Zend\View\Helper\AbstractHelper;

class Footertable extends AbstractHelper
{
    public function __invoke()
    {
        return 'bar';
    }
}

And use it in your views like this:

echo $this->footertable();

It should work.

Note: You also have to register all helpers (and other classes) in your module configuration's invokables section without spaces between the backslashes:

Wrong:

'footertable' => 'Application \ View \ Helper \ Footertable' 

Correct:

'footertable' => 'Application\View\Helper\Footertable'