Nesting Routes for AbstractRestfulController

classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|

Nesting Routes for AbstractRestfulController

tylkomat
I have a nested Route which points to two different AbstractRestfulControllers:

'router' => array(
    'routes' => array(
        'api' => array(
            'type' => 'Zend\Mvc\Router\Http\Literal',
            'options' => array(
                'route' => '/api',
            ),
            'may_terminate' => false,
            'child_routes' => array(
                'restCustomer' => array(
                    'type' => 'Zend\Mvc\Router\Http\Segment',
                    'options' => array(
                        'route' => '/customer[/:id]',
                        'defaults' => array(
                            'controller' => 'restCustomer',
                        ),
                        'constraints' => array(
                            'id' => '[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}',
                        ),
                    ),
                    'may_terminate' => true,
                    'child_routes' => array(
                        'latestArtifacts' => array(
                            'type' => 'Zend\Mvc\Router\Http\Literal',
                            'options' => array(
                                'route' => '/latest',
                                'defaults' => array(
                                    'controller' => 'restCustomerLatestArtifacts',
                                ),
                            ),
                            'may_terminate' => true,
                        ),
                    ),
                ),
            ),
        );
    ),
),

For the restCustomer route everything works as expected. /customer is mapped to getList() and /customer/:id is mapped to get($id) function. For the second controller the request goes to the get() function with the :id of the other controller. I would expected this request to go to getList().

Should I file a bug or did I do something wrong?
Reply | Threaded
Open this post in threaded view
|

Re: Nesting Routes for AbstractRestfulController

Alejandro Celaya
The second route is a child of /customer[/:id].
Instead make the parent route to be /customer with your current child and
another child with the /:id part.

--
Alejandro Celaya Alastrué
www.alejandrocelaya.com
El 30/3/2015 13:54, "tylkomat" <[hidden email]> escribió:

> I have a nested Route which points to two different
> AbstractRestfulControllers:
>
> 'router' => array(
>     'routes' => array(
>         'api' => array(
>             'type' => 'Zend\Mvc\Router\Http\Literal',
>             'options' => array(
>                 'route' => '/api',
>             ),
>             'may_terminate' => false,
>             'child_routes' => array(
>                 'restCustomer' => array(
>                     'type' => 'Zend\Mvc\Router\Http\Segment',
>                     'options' => array(
>                         'route' => '/customer[/:id]',
>                         'defaults' => array(
>                             'controller' => 'restCustomer',
>                         ),
>                         'constraints' => array(
>                             'id' =>
> '[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}',
>                         ),
>                     ),
>                     'may_terminate' => true,
>                     'child_routes' => array(
>                         'latestArtifacts' => array(
>                             'type' => 'Zend\Mvc\Router\Http\Literal',
>                             'options' => array(
>                                 'route' => '/latest',
>                                 'defaults' => array(
>                                     'controller' =>
> 'restCustomerLatestArtifacts',
>                                 ),
>                             ),
>                             'may_terminate' => true,
>                         ),
>                     ),
>                 ),
>             ),
>         );
>     ),
> ),
>
> For the restCustomer route everything works as expected. */customer* is
> mapped to *getList()* and */customer/:id* is mapped to *get($id)* function.
> For the second controller the request goes to the *get()* function with the
> *:id* of the other controller. I would expected this request to go to
> *getList()*.
>
> Should I file a bug or did I do something wrong?
>
>
>
> --
> View this message in context:
> http://zend-framework-community.634137.n4.nabble.com/Nesting-Routes-for-AbstractRestfulController-tp4662499.html
> Sent from the Zend Framework mailing list archive at Nabble.com.
>
> --
> List: [hidden email]
> Info: http://framework.zend.com/archives
> Unsubscribe: [hidden email]
>
>
>
Reply | Threaded
Open this post in threaded view
|

Re: Nesting Routes for AbstractRestfulController

tylkomat
I still need the id of the customer also in the other route, as I want
to display the artifacts of that specific customer.

As test I also added an id to the second route, which would look like
this: */customer[/:id]/latest[/:artifactId]*
I still the *:id* in the second controller.

Since its a new Controller, it is also a new scope. I would expect to
start a new controller with *getList()* as there is no *id *given for
the current controller.

Regards
Matthias Tylkowski

Am 30.03.2015 um 13:57 schrieb Alejandro Celaya:

> The second route is a child of /customer[/:id].
> Instead make the parent route to be /customer with your current child and
> another child with the /:id part.
>
> --
> Alejandro Celaya Alastrué
> www.alejandrocelaya.com
> El 30/3/2015 13:54, "tylkomat" <[hidden email]> escribió:
>
>> I have a nested Route which points to two different
>> AbstractRestfulControllers:
>>
>> 'router' => array(
>>      'routes' => array(
>>          'api' => array(
>>              'type' => 'Zend\Mvc\Router\Http\Literal',
>>              'options' => array(
>>                  'route' => '/api',
>>              ),
>>              'may_terminate' => false,
>>              'child_routes' => array(
>>                  'restCustomer' => array(
>>                      'type' => 'Zend\Mvc\Router\Http\Segment',
>>                      'options' => array(
>>                          'route' => '/customer[/:id]',
>>                          'defaults' => array(
>>                              'controller' => 'restCustomer',
>>                          ),
>>                          'constraints' => array(
>>                              'id' =>
>> '[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}',
>>                          ),
>>                      ),
>>                      'may_terminate' => true,
>>                      'child_routes' => array(
>>                          'latestArtifacts' => array(
>>                              'type' => 'Zend\Mvc\Router\Http\Literal',
>>                              'options' => array(
>>                                  'route' => '/latest',
>>                                  'defaults' => array(
>>                                      'controller' =>
>> 'restCustomerLatestArtifacts',
>>                                  ),
>>                              ),
>>                              'may_terminate' => true,
>>                          ),
>>                      ),
>>                  ),
>>              ),
>>          );
>>      ),
>> ),
>>
>> For the restCustomer route everything works as expected. */customer* is
>> mapped to *getList()* and */customer/:id* is mapped to *get($id)* function.
>> For the second controller the request goes to the *get()* function with the
>> *:id* of the other controller. I would expected this request to go to
>> *getList()*.
>>
>> Should I file a bug or did I do something wrong?
>>
>>
>>
>> --
>> View this message in context:
>> http://zend-framework-community.634137.n4.nabble.com/Nesting-Routes-for-AbstractRestfulController-tp4662499.html
>> Sent from the Zend Framework mailing list archive at Nabble.com.
>>
>> --
>> List: [hidden email]
>> Info: http://framework.zend.com/archives
>> Unsubscribe: [hidden email]
>>
>>
>>

Reply | Threaded
Open this post in threaded view
|

Re: Nesting Routes for AbstractRestfulController

Alejandro Celaya
Makes sense. I probably answered too fast.

--
Alejandro Celaya Alastrué
www.alejandrocelaya.com
El 30/3/2015 14:11, "Matthias Tylkowski" <[hidden email]> escribió:

> I still need the id of the customer also in the other route, as I want to
> display the artifacts of that specific customer.
>
> As test I also added an id to the second route, which would look like
> this: */customer[/:id]/latest[/:artifactId]*
> I still the *:id* in the second controller.
>
> Since its a new Controller, it is also a new scope. I would expect to
> start a new controller with *getList()* as there is no *id *given for the
> current controller.
>
> Regards
> Matthias Tylkowski
>
> Am 30.03.2015 um 13:57 schrieb Alejandro Celaya:
>
>> The second route is a child of /customer[/:id].
>> Instead make the parent route to be /customer with your current child and
>> another child with the /:id part.
>>
>> --
>> Alejandro Celaya Alastrué
>> www.alejandrocelaya.com
>> El 30/3/2015 13:54, "tylkomat" <[hidden email]> escribió:
>>
>>  I have a nested Route which points to two different
>>> AbstractRestfulControllers:
>>>
>>> 'router' => array(
>>>      'routes' => array(
>>>          'api' => array(
>>>              'type' => 'Zend\Mvc\Router\Http\Literal',
>>>              'options' => array(
>>>                  'route' => '/api',
>>>              ),
>>>              'may_terminate' => false,
>>>              'child_routes' => array(
>>>                  'restCustomer' => array(
>>>                      'type' => 'Zend\Mvc\Router\Http\Segment',
>>>                      'options' => array(
>>>                          'route' => '/customer[/:id]',
>>>                          'defaults' => array(
>>>                              'controller' => 'restCustomer',
>>>                          ),
>>>                          'constraints' => array(
>>>                              'id' =>
>>> '[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}',
>>>                          ),
>>>                      ),
>>>                      'may_terminate' => true,
>>>                      'child_routes' => array(
>>>                          'latestArtifacts' => array(
>>>                              'type' => 'Zend\Mvc\Router\Http\Literal',
>>>                              'options' => array(
>>>                                  'route' => '/latest',
>>>                                  'defaults' => array(
>>>                                      'controller' =>
>>> 'restCustomerLatestArtifacts',
>>>                                  ),
>>>                              ),
>>>                              'may_terminate' => true,
>>>                          ),
>>>                      ),
>>>                  ),
>>>              ),
>>>          );
>>>      ),
>>> ),
>>>
>>> For the restCustomer route everything works as expected. */customer* is
>>> mapped to *getList()* and */customer/:id* is mapped to *get($id)*
>>> function.
>>> For the second controller the request goes to the *get()* function with
>>> the
>>> *:id* of the other controller. I would expected this request to go to
>>> *getList()*.
>>>
>>> Should I file a bug or did I do something wrong?
>>>
>>>
>>>
>>> --
>>> View this message in context:
>>> http://zend-framework-community.634137.n4.nabble.com/Nesting-Routes-for-
>>> AbstractRestfulController-tp4662499.html
>>> Sent from the Zend Framework mailing list archive at Nabble.com.
>>>
>>> --
>>> List: [hidden email]
>>> Info: http://framework.zend.com/archives
>>> Unsubscribe: [hidden email]
>>>
>>>
>>>
>>>
>
Reply | Threaded
Open this post in threaded view
|

Re: Nesting Routes for AbstractRestfulController

tylkomat
Thanks that is the answer I was looking for. That should propably be
pointed out somewhere in the documentation (I hope I just didn't miss it).

Basically the default identifier should not be used at all or only at
the last child of the nesting, otherwise it will always occur on routes
where one only wants to have a list view.

Thanks also to Alejandro for your efforts and fast answers.

Regards
Matthias Tylkowski

Am 30.03.2015 um 14:52 schrieb Stefano Torresi:

> Hello Matthias,
>
> AbstractRestfulController::dispatch() method determines what method to
> invoke on the basis of request method and the presence, or lack
> thereof, of the identifier parameter in the RouteMatch.
>
> With nested routes, that means that you should set a different id
> parameter name for each controller, as the
> default AbstractRestfulController:$identifierName value is simply
> 'id', and that can result in name conflicts.
>
> Stefano Torresi
> Web Developer