我有自定义路由器,我必须访问 Zend\Navigation
在此自定义路由器内。我在谷歌上搜索、询问和搜索,但没有结果:/
我需要的只是在 Alias::match 函数中使用 Zend\Navigation 查找具有“link”参数的节点。
这是我的 module.config.php:
'navigation' => array(
'default' => array(
'account' => array(
'label' => 'Account',
'route' => 'node',
'pages' => array(
'home' => array(
'label' => 'Dashboard',
'route' => 'node',
'params' => array(
'id' => '1',
'link' => '/about/gallery'
),
),
),
),
),
),
[...]
这是我的 Alias 类:
// file within ModuleName/src/ModuleName/Router/Alias.php
namespace Application\Router;
use Traversable;
use Zend\Mvc\Router\Exception;
use Zend\Stdlib\ArrayUtils;
use Zend\Stdlib\RequestInterface as Request;
use Zend\Mvc\Router\Http;
use Zend\ServiceManager\ServiceLocatorAwareInterface;
use Zend\ServiceManager\ServiceLocatorInterface;
class Alias extends Http\Segment implements ServiceLocatorAwareInterface
{
public function setServiceLocator(ServiceLocatorInterface $serviceLocator)
{
$this->serviceLocator = $serviceLocator;
return $this;
}
public function getServiceLocator()
{
return $this->serviceLocator;
}
public function match(Request $request, $pathOffset = null)
{
[...]
return parent::match($request, $pathOffset);
}
}
编辑:
现在我知道我应该将服务管理器注入(inject)到我的自定义路由器中。如果您知道如何执行此操作,请告诉我:)
编辑:
好吧,它不是自定义路由器,而是路由。我的错。我正在谈论#zftalk
irc Chanel 和 AliasSegment
类应该实现ServiceLocatorAwareInterface
。好的,我已经尝试过了,但现在还有另一个问题。 罢工>
在 setServiceLocator
我无法获得的功能service locator
。它返回 null 对象,但是 $serviceLocator
是类(class) Zend\Mvc\Router\RoutePluginManager
.
public function setServiceLocator(ServiceLocatorInterface $serviceLocator){
$sl = $serviceLocator->getServiceLocator();
var_dump($sl); // NULL
}
有什么想法如何从中获取 Zend 导航吗?
已编辑
与@mmmshuddup所说的相对应,我已经更改了我的自定义路由器类。 (上面是新版本)。也在我的Module.php
,在onBootstrap
内函数,我添加了这一行:
$sm->setFactory('Navigation', 'Zend\Navigation\Service\DefaultNavigationFactory', true);
导航工作及其之前实例化route
所以它应该在我的 Alias 类中可见,但事实并非如此。
我已将其放入我的 match
函数位于 Alias
将此行类:
$servicesArray = $this->getServiceLocator()->getRegisteredServices();
和$servicesArray
几乎是空的。没有服务,没有工厂。同一行插入 onBootstrap
,在设置新工厂(如上所述)后返回数组 navigation
和其他服务。
问题是:我如何与我的自定义路由器共享此数组(或 ServiceManager):Alias
?
我不得不说,我想做的一切都可以在 ZF1 中实现,而且非常简单。
编辑
我找到了解决方案。答案如下
最佳答案
那是因为该对象本身确实没有any properties declared 。但如果你这样做:
echo get_class($sl);
你会看到它确实是 Zend\ServiceManager\ServiceManager
的一个实例
您应该能够通过执行以下操作来获取导航实例:
$nav = $sl->get('Navigation');
罢工><罢工>罢工>
编辑:
我只是注意到你的代码的错误位置有一些东西。您正在调用getServiceLocator()
于 $serviceLocator
这已经是这样的例子了。您还在 setServiceLocator()
中调用它。您应该将其更改为:
// EDIT - file within ModuleName/src/Router/Alias.php
namespace Application\Router;
use Traversable;
use Zend\Mvc\Router\Exception;
use Zend\Stdlib\ArrayUtils;
use Zend\Stdlib\RequestInterface as Request;
use Zend\Mvc\Router\Http;
class Alias extends Http\Segment implements ServiceLocatorAwareInterface
{
public function setServiceLocator(ServiceLocatorInterface $serviceLocator)
{
$this->serviceLocator = $serviceLocator;
return $this;
}
public function getServiceLocator()
{
return $this->serviceLocator;
}
public function match(Request $request, $pathOffset = null)
{
$nav = $this->getServiceLocator()->get('Navigation');
// ...
return parent::match($request, $pathOffset);
}
}
关于php - ZF2 : How to get Zend\Navigation inside custom route?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13324022/