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

标签 php zend-framework2 zend-navigation zend-router

我有自定义路由器,我必须访问 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/

相关文章:

php - 如何删除错误 "Trying to get property of non-object"?

php - 如何从 mysql 填充下拉列表值

php - 如何修复 Class 'Album\Controller\AlbumController' not found 错误?

zend-framework - 如果父项和子项具有相同的 URI 并使用 Zend_Navigation,如何在菜单中呈现事件的父项和子项?

zend-framework - Zend_Navigation_Container 设置分隔符

PHP 日历在新 div 中包含过期日期

php - WooCommerce 不会在发送给客户的电子邮件中显示自定义费用

module - ZF2 : Get module name (or route) in the application layout for menu highlight

zend-framework2 - 带有自定义验证器的 PHPUnit ZF2 输入过滤器

php - Zend 导航和 Zend ACL