通过 ServiceManager 使用 View Helper 注入自定义导航 [英] Injecting custom navigation using a View Helper through the ServiceManager
问题描述
我刚刚开始为 Zend Framework 2 进行开发.我正在尝试向我的应用程序添加一个简单的菜单.菜单最终将作为用户可定义的书签从数据库中加载,所以目前,我正在尝试实例化我定义的视图助手,在控制器中以编程方式添加页面,然后将视图助手的导航注入到视图中模型.我的问题是,当我尝试使用 ServiceLocator 在控制器中检索我的视图助手时,我得到一个 ServiceNotFoundException
:
I'm just starting out developing for Zend Framework 2. I'm trying to add a simple menu to my Application. The menu will eventually be loaded from a database as user-definable bookmarks, so at the moment, I am trying to instantiate a view helper I've defined, add pages programmatically in the controller, and then inject the view helper's navigation into the view model. My problem is that when I try to retrieve my view helper in the controller by using the ServiceLocator, I get a ServiceNotFoundException
:
Application\View\Helper\ShortcutsMenu:
Application\View\Helper\ShortcutsMenu:
namespace Application\View\Helper;
use Zend\Navigation\Navigation;
class ShortcutsMenu extends Navigation {
public function shortcutsMenu() {
//...
}
public function __construct() {
//...
}
}
在 Module.php 中
and in Module.php
public function getServiceConfig() {
return array(
'view_helper' => array(
'factories' => array(
'shortcutsmenu' => function($sm) {
$smenu = new \Application\View\Helper\ShortcutsMenu();
return $smenu;
}
),
),
);
IndexController.php:
IndexController.php:
$smenu = $this->getServiceLocator()->get('shortcutsmenu'); // throws ServiceNotFoundException
//"Zend\ServiceManager\ServiceManager::get was unable to fetch or create an instance for shortcutsmenu"
$smenu->addPage(AbstractPage::factory(array(
'label' => 'Homepage',
'order' => '-1',
'uri' => '/',
)));
// ...
}
谁能告诉我我错过了什么?
Can anyone tell me what I'm missing?
我想在应用程序范围的布局中生成的 HTML 类似于:
The HTML I would like to generate in the application-wide layout would be something like:
<!-- Side tabs shortcuts -->
<ul id="shortcuts">
<li class="current"><a href="./" class="shortcut-home" title="Home">Home</a></li>
<li><a href="userpage1.html" title="My messages">My messages</a></li>
<li><a href="/a/b/c?id=4" title="Bob's calendar">Bob's calendar</a></li>
<li>...</li>
</ul>
可能使用 URI 样式的链接而不是 MVC 链接.
probably using URI-style links rather than MVC ones.
推荐答案
无需扩展导航容器 Zend\Navigation\Navigation
或扩展内置视图助手来渲染菜单.
There is no need to extend the navigation container Zend\Navigation\Navigation
or extend the builtin view helpers to render menu's.
>
容器 管理导航中的所有页面结构体.有几种方法可以创建容器.
A container manages all pages in the navigation structure. The are several ways to create a container.
所有查看助手(菜单、面包屑)使用容器作为导航数据的提供者.您可以使用 setContainer()
在视图助手上设置一个新容器.或者,您可以在没有容器设置的情况下在视图中调用视图助手,视图助手将为您创建一个新的空容器.
All the view helpers (menu, breadcrumbs) use the container as the provider for navigation data. You can eighter set a new container on the view helper using setContainer()
. Alternatively you could just call the view helper in your view without a container setup and the view helper will create a new empty container for you.
如果由于默认视图助手未提供而需要一些替代渲染,您可以创建自己的导航视图助手.
If you need some alternate rendering because the default view helpers don't provide it you can create you own navigation view helper.
namespace MyNamespace\View\Helper\Navigation;
use Zend\View\Helper\Navigation\AbstractHelper;
class MyHelper extends AbstractHelper
{
}
接下来将您的视图助手注册到导航插件管理器.我认为你可以做这样的事情(未经测试):
Next register your view helper to the navigation pluginManager. I think you can do something like this (untested):
class Module
{
public function onBootstrap($e)
{
$application = $e->getApplication();
/** @var $serviceManager \Zend\ServiceManager\ServiceManager */
$serviceManager = $application->getServiceManager();
$pm = $serviceManager->get('ViewHelperManager')->get('Navigation')->getPluginManager();
$pm->setInvokableClass('myHelper', 'MyNamespace\View\Helper\Navigation\MyHelper');
}
}
现在在您的视图中调用自定义助手:
Now call you custom helper in your view:
$this->navigation()->myHelper()
这篇关于通过 ServiceManager 使用 View Helper 注入自定义导航的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!