问题描述
在我的控制器中,我创建了 Navigation 对象并将其传递给视图
In my controller I create the Navigation object and passing it to the view
$navigation = new \Zend\Navigation\Navigation(array(
array(
'label' => 'Album',
'controller' => 'album',
'action' => 'index',
'route' => 'album',
),
));
尝试使用它
<?php echo $this->navigation($this->navigation)->menu() ?>
并得到错误:
致命错误:Zend\Navigation\Exception\DomainException:Zend\Navigation\Page\Mvc::getHref 无法执行,因为 Zend\View\Helper\Navigation\AbstractHelper.php 中没有 Zend\Mvc\Router\RouteStackInterface 实例在线 471
但是我在布局中使用的导航,就像这里写的那样:http://adam.lundrigan.ca/2012/07/quick-and-dirty-zf2-zend-navigation/ 有效.我的错误是什么?
But navigation which I use in layout, so as it is written here: http://adam.lundrigan.ca/2012/07/quick-and-dirty-zf2-zend-navigation/ works. What is my mistake?
谢谢.
推荐答案
问题是缺少路由器(或者更准确地说,是一个 Zend\Mvc\Router\RouteStackInterface
).路由堆栈是路由的集合,可以使用路由名称将其转换为 url.基本上它接受一个路由名称并为你创建一个 url:
The problem is a missing Router (or to be more precise, a Zend\Mvc\Router\RouteStackInterface
). A route stack is a collection of routes and can use a route name to turn that into an url. Basically it accepts a route name and creates an url for you:
$url = $routeStack->assemble('my/route');
这也发生在 Zend\Navigation
的 MVC 页面中.页面有一个 route
参数,当有可用的路由器时,页面会组合它自己的 url(或者在 Zend\Navigation
术语中,一个 href
).如果不提供路由器,则无法组装路由,从而引发异常.
This happens inside the MVC Pages of Zend\Navigation
too. The page has a route
parameter and when there is a router available, the page assembles it's own url (or in Zend\Navigation
terms, an href
). If you do not provide the router, it cannot assemble the route and thus throws an exception.
必须在导航的每个页面中注入路由器:
You must inject the router in every page of the navigation:
$navigation = new Navigation($config);
$router = $serviceLocator->get('router');
function injectRouter($navigation, $router) {
foreach ($navigation->getPages() as $page) {
if ($page instanceof MvcPage) {
$page->setRouter($router);
}
if ($page->hasPages()) {
injectRouter($page, $router);
}
}
}
如您所见,它是一个递归函数,将路由器注入每个页面.乏味!因此,有一家工厂可以为您做这件事.有四个简单的步骤来实现这一目标.
As you see it is a recursive function, injecting the router into every page. Tedious! Therefore there is a factory to do this for you. There are four simple steps to make this happen.
第一步
首先将导航配置放入您的模块配置中.就像您拥有 default
导航一样,您可以创建第二个 secondary
.
Put the navigation configuration in your module configuration first. Just as you have a default
navigation, you can create a second one secondary
.
'navigation' => array(
'secondary' => array(
'page-1' => array(
'label' => 'First page',
'route' => 'route-1'
),
'page-2' => array(
'label' => 'Second page',
'route' => 'route-2'
),
),
),
您有到第一页 (route-1
) 和第二页 (route-2
) 的路由.
You have routes to your first page (route-1
) and second page (route-2
).
第二步
工厂会将其转换为导航对象结构,您需要先为此创建一个类.在 MyModule/Navigation/Service 目录中创建一个文件 SecondaryNavigationFactory.php
.
A factory will convert this into a navigation object structure, you need to create a class for that first. Create a file SecondaryNavigationFactory.php
in your MyModule/Navigation/Service directory.
namespace MyModule\Navigation\Service;
use Zend\Navigation\Service\DefaultNavigationFactory;
class SecondaryNavigationFactory extends DefaultNavigationFactory
{
protected function getName()
{
return 'secondary';
}
}
看我把名字secondary
放在这里,和你的导航键一样.
See I put the name secondary
here, which is the same as your navigation key.
第三步
您必须向服务经理注册此工厂.然后工厂可以完成它的工作并将配置文件转换为 Zend\Navigation
对象.您可以在 module.config.php 中执行此操作:
You must register this factory to the service manager. Then the factory can do it's work and turn the configuration file into a Zend\Navigation
object. You can do this in your module.config.php:
'service_manager' => array(
'factories' => array(
'secondary_navigation' => 'MyModule\Navigation\Service\SecondaryNavigationFactory'
),
)
看到我在这里创建了一个服务 secondary_navigation
,然后工厂将返回一个 Zend\Navigation
实例.如果您现在执行 $sm->get('secondary_navigation')
,您将看到这是一个 Zend\Navigation\Navigation
对象.
See I made a service secondary_navigation
here, where the factory will return a Zend\Navigation
instance then. If you do now $sm->get('secondary_navigation')
you will see that is a Zend\Navigation\Navigation
object.
第四步
告诉视图助手使用此导航而不是默认导航.导航视图助手接受导航"参数,您可以在其中说明所需的导航.在这种情况下,服务管理器有一个服务 secondary_navigation
,这就是我们需要的.
Tell the view helper to use this navigation and not the default one. The navigation view helper accepts a "navigation" parameter where you can state which navigation you want. In this case, the service manager has a service secondary_navigation
and that is the one we need.
<?= $this->navigation('secondary_navigation')->menu() ?>
现在您将拥有此视图助手中使用的导航secondary
.
Now you will have the navigation secondary
used in this view helper.
披露:这个答案与我在这个问题上给出的答案相同:https://stackoverflow.com/a/12973806/434223
这篇关于Zend 框架 2:导航的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!