Create an instance of Url View Helper in another ViewHelper

核能气质少年 提交于 2019-12-08 07:00:43

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!