Issues

ZF2-105: Zend\Mvc\Router\Http\Part child routes should be an array

Description

I have the following child route configured, following examples I found:


    'routes' => array(
        'user-admin' => array(
            'type' => 'Literal',
            'options' => array(
                'route'    => '/admin/users',
                'defaults' => array(
                    'controller' => 'user-admin',
                    'action'     => 'index',
                ),
            ),
            'may_terminate' => TRUE,
            'child_routes' => array(
                'create' => array(
                    'type' => 'Literal',
                    'options' => array(
                        'route'    => 'create',
                        'defaults' => array(
                            'controller' => 'user-admin',
                            'action'     => 'edit',
                        ),
                    ),
                ),
            ),
        ),

But that generates the following exception:


"( ! ) Catchable fatal error: Argument 4 passed to Zend\Mvc\Router\Http\Part::__construct() must be an array, object given, called in /home/vhosts/zf2soccer.dev/library/ZendFramework/library/Zend/Mvc/Router/Http/Part.php on line 131 and defined in /home/vhosts/zf2soccer.dev/library/ZendFramework/library/Zend/Mvc/Router/Http/Part.php on line 75"

It seems that the Zend\Mvc\Router\Http\Part::factory() method does not convert the whole configuration to an array, as the $option['child_routes'] is an instance of Zend\Config\Config in my case.

The following patch seems to fix this, by converting $options['child_routes'] to an array before invoking the constructor.


diff --git a/library/Zend/Mvc/Router/Http/Part.php b/library/Zend/Mvc/Router/Http/Part.php
index f800cbc..b5c1be1 100644
--- a/library/Zend/Mvc/Router/Http/Part.php
+++ b/library/Zend/Mvc/Router/Http/Part.php
@@ -123,6 +123,9 @@ class Part extends TreeRouteStack implements Route
         if (!isset($options['child_routes']) || !$options['child_routes']) {
             $options['child_routes'] = null;
         }
+        else if ($options['child_routes'] instanceof Traversable && !$options['child_routes'] instanceof ArrayAccess) {
+            $options['child_routes'] = IteratorToArray::convert($options['child_routes']);
+        }
 
         return new static($options['route'], $options['may_terminate'], $options['route_broker'], $options['child_routes']);
     }

Comments

Added code tags

Merged to master.