问题
In my project i created a Helper to display a-tags, but with an additonal check, if the user has ACL rights. When he has access to the url, i want to create the Url with the existing UrlHelper - but i can't instantiate it:
// try 1
$pluginHelper = \Zend\Mvc\Service\ViewHelperManagerFactory::createService($this->getServiceLocator());
$this->_urlHelper = $pluginHelper->get('url');
// try 2
$factory = new \Zend\Mvc\Service\RouterFactory();
$router = $this->_urlHelper = $factory->createService($this->getServiceLocator());
// try 3
$module = new \RDGOnline\Module();
$config = $module->getConfig();
$router = \Zend\Mvc\Router\Http\TreeRouteStack::factory($config['router']);
$this->_urlHelper = new Url();
$this->_urlHelper->setRouter($router);
I attempted different ways, but all failed. In my last try the url was created - but it ignores the subfolder. I.E. /trunk/
Thanks for Helping.
回答1:
If you're doing this inside a view helper, you can access view helpers (plugins) through the injected view's plugin method
public function someViewHelperMethod()
{
$urlHelper = $this->view->plugin('url');
}
来源:https://stackoverflow.com/questions/14560219/create-an-instance-of-url-view-helper-in-another-viewhelper