Issues

ZF-12544: Invalid behavior when loading pages from file thru ConstructedNavigationFactory page will not become active

Description

I have all my pages inside separate main_menu.xml file. In my module.config.php i have include navigation in this way:


 'invokables' => array(
    ...
   'about' => 'Front\Controller\AboutController',
    ....
 ),
 'routes' => array(
   ....
   'front-about' => array(
            'type' => 'Zend\Mvc\Router\Http\Segment',
            'options' => array(
                'route' => '/about',
                'defaults' => array('controller' => 'about')
            )
   )
   ....
 ),
 'service_manager' => array(
        'factories' => array(
            ......
            'navigation' => function ($serviceManager) {
                $conf = __DIR__ . '/../navigation/main_menu.xml';
                $factory = new \Zend\Navigation\Service\ConstructedNavigationFactory($conf);
                return $factory->createService($serviceManager);
            }
            .......
        )
    )

<?xml version="1.0" encoding="UTF-8"?>
About
      aboutfront-about
   ......

I have next error: {quote}Fatal error: Zend\Navigation\Exception\DomainException: Zend\Navigation\Page\Mvc::getHref cannot execute as no Zend\Mvc\Router\RouteStackInterface instance is composed{quote}

I found work around, just set default router for Page/MVC (It is not good solution, but it was helped):


 'service_manager' => array(
        'factories' => array(
            ......
            'navigation' => function ($serviceManager) {
                $conf = __DIR__ . '/../navigation/main_menu.xml';
                $factory = new \Zend\Navigation\Service\ConstructedNavigationFactory($conf);
                \Zend\Navigation\Page\Mvc::setDefaultRouter($serviceManager->get('router'));
                return $factory->createService($serviceManager);
            }
            .......
        )
    )

But now when you will open page /about, the Page\Mvc->isActive is false.

My way to fix this issue:


From 98342a68f982d69c5315cfb07c4be2522dbac657 Mon Sep 17 00:00:00 2001
From: Milotskiy Alexey <....>
Date: Sun, 17 Mar 2013 16:42:21 +0200
Subject: [PATCH] Fixed issue with ConstructedNavigationFactory

---
 Navigation/Service/AbstractNavigationFactory.php   |   20 +++++++++++++++-----
 .../Service/ConstructedNavigationFactory.php       |   10 +++++++++-
 2 files changed, 24 insertions(+), 6 deletions(-)

diff --git a/Navigation/Service/AbstractNavigationFactory.php b/Navigation/Service/AbstractNavigationFactory.php
index 89368e8..f5af421 100644
--- a/Navigation/Service/AbstractNavigationFactory.php
+++ b/Navigation/Service/AbstractNavigationFactory.php
@@ -63,17 +63,27 @@ abstract class AbstractNavigationFactory implements FactoryInterface
                 ));
             }
 
-            $application = $serviceLocator->get('Application');
-            $routeMatch  = $application->getMvcEvent()->getRouteMatch();
-            $router      = $application->getMvcEvent()->getRouter();
             $pages       = $this->getPagesFromConfig($configuration['navigation'][$this->getName()]);
-
-            $this->pages = $this->injectComponents($pages, $routeMatch, $router);
+            $this->pages = $this->preparePages($serviceLocator, $pages);
         }
         return $this->pages;
     }
 
     /**
+     * @param ServiceLocatorInterface $serviceLocator
+     * @param array|\Zend\Config\Config $pages
+     * @throws \Zend\Navigation\Exception\InvalidArgumentException
+     */
+    protected function preparePages(ServiceLocatorInterface $serviceLocator, $pages)
+    {
+        $application = $serviceLocator->get('Application');
+        $routeMatch  = $application->getMvcEvent()->getRouteMatch();
+        $router      = $application->getMvcEvent()->getRouter();
+
+        return $this->injectComponents($pages, $routeMatch, $router);
+    }
+
+    /**
      * @param string|\Zend\Config\Config|array $config
      * @return array|null|\Zend\Config\Config
      * @throws \Zend\Navigation\Exception\InvalidArgumentException
diff --git a/Navigation/Service/ConstructedNavigationFactory.php b/Navigation/Service/ConstructedNavigationFactory.php
index bbf75fa..373c588 100644
--- a/Navigation/Service/ConstructedNavigationFactory.php
+++ b/Navigation/Service/ConstructedNavigationFactory.php
@@ -17,11 +17,16 @@ use Zend\ServiceManager\ServiceLocatorInterface;
 class ConstructedNavigationFactory extends AbstractNavigationFactory
 {
     /**
+     * @var string|\Zend\Config\Config|array
+     */
+    protected $config;
+
+    /**
      * @param string|\Zend\Config\Config|array $config
      */
     public function __construct($config)
     {
-        $this->pages = $this->getPagesFromConfig($config);
+        $this->config = $config;
     }
 
     /**
@@ -30,6 +35,9 @@ class ConstructedNavigationFactory extends AbstractNavigationFactory
      */
     public function getPages(ServiceLocatorInterface $serviceLocator)
     {
+        if (null === $this->pages) {
+            $this->pages = $this->preparePages($serviceLocator, $this->getPagesFromConfig($this->config));
+        }
         return $this->pages;
     }
 
-- 

Comments

Please move it to ZF2, because i have not found the way to done it.

Please report all issues for ZF2 on Github.