Advanced Routing

I want to achieve this kind of routing:

ParentRouteController
GET domain.com/parent-route
GET domain.com/parent-route/:value
POST domain.com/parent-route
UPDATE domain.com/parent-route/:value

ChildRoute1Controller
GET domain.com/parent-route/:value/child-route-1
GET domain.com/parent-route/:value/child-route-1/:child1value
POST domain.com/parent-route/:value/child-route-1
UPDATE domain.com/parent-route/:value/child-route-1/:child1value

ChildRoute2Controller
GET domain.com/parent-route/:value/child-route-1/:child1value/child-route-2
GET domain.com/parent-route/:value/child-route-1/:child1value/child-route-2/:childRoute2Value
POST domain.com/parent-route/:value/child-route-1/:child1value/child-route-2
UPDATE domain.com/parent-route/:value/child-route-1/:child1value/child-route-2/:childRoute2Value

I’m using ZF 2.5.2, thanks!

Seems quite straightforward: besides the HTTP method (which will probably be your leaf route), it is nested child routes.

What did you try so far?

I tried something like this:

<?php
return [
    'router' => [
        'routes' => [
            'parent-route' => [
                'type' => 'segment',
                'options' => [
                    'route' => '/parent-route[/:id]',
                    'defaults' => [
                        'controller' => 'MyModule\Controller\Rest\First',
                    ],
                ],
            ],
            'may_terminate' => false,
            'child_routes' => [
                'child-route-1' => [
                    'type' => 'segment',
                    'options' => [
                        'route' => '/parent-route[/:id]/child-route-1[/:child_route1_id]]',
                        'defaults' => [
                            'controller' => 'MyModule\Controller\Rest\Second',
                        ],
                    ],
                    'may_terminate' => false,
                    'child_routes' => [
                        'child-route-2' => [
                            'type' => 'segment',
                            'options' => [
                                'route' => '/parent-route[/:id]/child-route-1[/:child_route1_id]/child-route-2[/:child_route2_id]]',
                                'defaults' => [
                                    'controller' => 'MyModule\Controller\Rest\Third',
                                ],
                            ],
                        ],
                    ]
                ],
            ]
        ],
    ],
    'controllers' => [
        'invokables' => [
            'MyModule\Controller\Rest\First'  => 'MyModule\Controller\Rest\FirstController',
            'MyModule\Controller\Rest\Second' => 'MyModule\Controller\Rest\SecondController',
            'MyModule\Controller\Rest\Third'  => 'MyModule\Controller\Rest\ThirdController'
        ],
    ],
];

When i accessed the route:

domain.com/parent-route/123/child-route-1/456

It goes to the SecondController::getList() function of my Rest Controller instead of SecondController::get($value).

Check the nesting of those arrays: your second route doesn’t seem to be under the first one. Alao, the optional bits (like [/:id]) can’t work like that, as they can ambiguously match either an ID or a child route.

Thanks @ocramius,

I finally figured it out. The :id was ambiguous and needs to be unique for the parent and child routes.