I wrote a simple url view helper, that extends Zend\View\Helper\Url
and attached it to the ViewHelperManager
:
MyNamespace\View\Helper\Url
namespace MyNamespace\View\Helper;
use Zend\View\Helper\Url as ZendUrl;
class Url extends ZendUrl {
public function __invoke($name = null, array $params = array(), $options = array(), $reuseMatchedParams = false) {
$link = parent::__invoke($name, $params, $options, $reuseMatchedParams);
...
return $link;
}
}
Application\Module
namespace Application;
use ...
class Module {
public function onBootstrap(MvcEvent $mvcEvent) {
$application = $mvcEvent->getApplication();
$serviceManager = $application->getServiceManager();
$viewHelperManager = $serviceManager->get('ViewHelperManager');
$viewHelperManager->setInvokableClass('url', 'MyNamespace\View\Helper\Url');
...
}
}
Now the application throws an exception:
Fatal error: Uncaught exception 'Zend\View\Exception\RuntimeException' with message 'No RouteStackInterface instance provided' in /var/www/foo/bar/vendor/zendframework/zendframework/library/Zend/View/Helper/Url.php on line 76
Zend\View\Exception\RuntimeException: No RouteStackInterface instance provided in /var/www/foo/bar/vendor/zendframework/zendframework/library/Zend/View/Helper/Url.php on line 76
I debugged both Url
classes. Wenn MyNamespace\View\Helper\Url
is used, the method Zend\View\Helper\Url#setRouter(...)
is not called and the router is not set. Don't get why...
How to get it working?
You can also use initializers!
And You can extend \Zend\View\Helper\Url as You want it
Not tested this so I don't know if it works, I am just guessing:
Replace:
with:
Though this questions is quite old, figured it might be worth adding a configuration based override here as well using a factory registered with the
HelperPluginManager
:Inside the
module.config.php
:And the view helper plugin factory itself:
The trick here really is that in this factory, one only receives the
HelperPluginManager
service locator. To access other services, one needs to fetch the global service locator first (which is done by theuse
ing the global service locator from outside the closure in all other solutions).Aydin's solution worked for me, I put the code, more or less untouched in the getViewHelperConfig of the module.php