custom view helper error in zend framework 2
-
12-12-2019 - |
Question
I cannot seem to register a custom view helper in zend Framework 2.02 I tried all solutions posted here and anything I can think I should do but I keep getting this error:
Fatal error: Class 'ModuleName\view\Helper\mylinkhelper' not found in C:\wamp\vhosts\projectName\vendor\zendframework\zendframework\library\Zend\ServiceManager\AbstractPluginManager.php on line 177
And here's how my module.config.php looks like:
return array{
'controllers'=>array(
....
),
'view_manager' => array(
'template_path_stack' => array(
'ModuleName' => __DIR__ . '/../view',
),
),
'view_helpers' => array(
'invokables' => array(
'mylink' => 'ModuleName\view\Helper\mylinkhelper',
),
),
};
in my view file, I have:
echo $this->mylink($someparameter);
I appreciate any feedback on this. I don't really know what else to do here.
Solution
<?php
// ./module/Application/src/Application/View/Helper/AbsoluteUrl.php
namespace Application\View\Helper;
use Zend\Http\Request;
use Zend\View\Helper\AbstractHelper;
class AbsoluteUrl extends AbstractHelper
{
protected $request;
public function __construct(Request $request)
{
$this->request = $request;
}
public function __invoke()
{
return $this->request->getUri()->normalize();
}
}
You’ll notice that this particular helper has a dependency — a Zend\Http\Request object. To inject this, we’ll need to set up a factory with the initialization logic for our view helper:
<?php
// ./module/Application/Module.php
namespace Application;
use Application\View\Helper\AbsoluteUrl;
class Module
{
public function getViewHelperConfig()
{
return array(
'factories' => array(
// the array key here is the name you will call the view helper by in your view scripts
'absoluteUrl' => function($sm) {
$locator = $sm->getServiceLocator(); // $sm is the view helper manager, so we need to fetch the main service manager
return new AbsoluteUrl($locator->get('Request'));
},
),
);
}
// If copy/pasting this example, you'll also need the getAutoloaderConfig() method; I've omitted it for the sake of brevity.
}
That’s it! Now you can call your helper in your view scripts:
The full URL to the current page is: <?php echo $this->absoluteUrl(); ?>
thanks to evan to create this tutorial
OTHER TIPS
It looks like the View Helper is correctly added to the ServiceManager
since invoking mylink()
is trying to create ModuleName\view\Helper\mylinkhelper
.
I'd make sure the class is creatable with new College\view\Helper\mylinkhelper();
from a controller, this is likely to throw up some clues. Also check the filename and classname are correct.
Your approach is correct, but there might be two things which cause you this trouble:
You talk about a top level namespace
ModuleName
, but in your example configuration you have the top level namespaceCollege
. When you have aModuleName
namespace and you try to loadCollege
, that obviously does not workYour view helper cannot be autoloaded. Are you sure the class name is correct (
MyLinkHelper
), the namespace is correct (College\View\Helper
, see also above) and the file name is correct (MyLinkHelper.php
). And have you enabled class-name autoloading for this module in your module class?
A third option might be the lower case "view" and "mylinkhelper" as usually you would write College\View\Helper\MyLinkHelper
with a capital V, M, L and H. But since you are on Windows that should not matter afaik. I know for Linux you must be aware of case sensitiveness of class names.
The problem is that the class file is not being loaded. Its supposed to be included in autoload_classmap.php.
<?php
return array(
'{module}\View\Helper\{helper}' => __DIR__ . '\View\Helper\{helper}.php',
);
?>
I ran in the same issue and this page helped me.
As I'm new to ZF, i don't know if there is another way to add the paths in autoload_classmap, i think probably there is, but i just edited the file manually.
Got the same problem, found out by myself that view helper file was not included. while putting it into controller for testing it worked
e.g.: require_once('module/Pages/view/Helper/RenderNav.php');
why it has not been autoloaded ?