gpt4 book ai didi

php - ZF2 : How to get Zend\Navigation inside custom route?

转载 作者:行者123 更新时间:2023-12-04 05:24:40 24 4
gpt4 key购买 nike

我有自定义路由器,我必须访问 Zend\Navigation在这个自定义路由器里面。我在谷歌上搜索、询问和搜索,但没有结果:/

我只需要在 Alias::match 函数中使用 Zend\Navigation 找到具有“链接”参数的节点。

这是我的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'
),
),
),
),
),
),
[...]

这是我的别名类(class):
// 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 chanell 和 AliasSegment类应该实现 ServiceLocatorAwareInterface .好的,我已经尝试过了,但现在还有另一个问题。

setServiceLocator我无法获得的功能 service locator .它返回空对象,但是 $serviceLocator是类 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com