如何从全局角度访问headLink
视图助手,例如:
public function onBootstrap(MvcEvent $e)
{
$eventManager = $e->getApplication()->getEventManager();
$moduleRouteListener = new ModuleRouteListener();
$moduleRouteListener->attach($eventManager);
$app = $e->getTarget();
$locator = $app->getServiceManager();
$view = $locator->get('Zend\View\View');
$view->plugin()->headLink()->appendStylesheet('img.css');
}
另一个很好的例子是,如果我使用一个事件来触发一个可以访问视图助手的自定义类。我想我需要访问application
类。
请尽可能协助。
答案 0 :(得分:0)
public function onBootstrap(MvcEvent $e)
{
$serviceManager = $e->getApplication()->getServiceManager();
$headLinkHelper = $serviceManager->get('ViewHelperManager')->get('HeadLink');
$headLinkHelper->appendStylesheet('img.css');
}