在另一个 ViewHelper 中创建 Url View Helper 的实例 [英] Create an instance of Url View Helper in another ViewHelper
问题描述
在我的项目中,我创建了一个 Helper 来显示 a-tags,但如果用户具有 ACL 权限,还需要额外检查.当他可以访问 url 时,我想使用现有的 UrlHelper 创建 Url - 但我无法实例化它:
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);
我尝试了不同的方法,但都失败了.在我最后一次尝试中,创建了 url - 但它忽略了子文件夹.IE./主干/
I attempted different ways, but all failed. In my last try the url was created - but it ignores the subfolder. I.E. /trunk/
感谢您的帮助.
推荐答案
如果你在一个视图助手中这样做,你可以通过注入视图的插件方法访问视图助手(插件)
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');
}
这篇关于在另一个 ViewHelper 中创建 Url View Helper 的实例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!