laminas-api-tools / api-tools-hal

Laminas Module providing Hypermedia Application Language assets and rendering
https://api-tools.getlaminas.org/documentation
BSD 3-Clause "New" or "Revised" License
6 stars 12 forks source link

Dynamic route params for collections using the metadata_map #11

Open weierophinney opened 4 years ago

weierophinney commented 4 years ago

When using the metadata_map to create further links, there is a problem with the route parameters.

Following config:

    'zf-hal' => [
        'metadata_map' => [
            'List\\List\\List' => [
                'route_name' => 'lists.rest.list',
                'route_identifier_name' => 'listId',
                'entity_identifier_name' => 'id',
                'links' => [
                    [
                        'rel' => 'reference-list',
                        'route'=> [
                            'name' => 'lists.rest.referencelist',
                        ],
                    ],
                ],
            ],
        ],
    ],
    'router' => [
        'routes' => [
            'lists.rest.list' => [
                'type' => 'segment',
                'options' => [
                    'route' =>  '/rest/list[/:listId]',
                ],
            ],
            'lists.rest.referencelist' => [
                'type' => 'segment',
                'options' => [
                    'route' =>  '/rest/list/:listId/reference[/:referenceId]',
                ],
            ],
        ],
    ],

If you query a single resource from lists.rest.list (/rest/list/1) the links will generated perfectly.
But if you query the collection from lists.rest.list (/rest/list) you will receive an error.

The reason is, that in this case the route param listId is unknown.
I thought, that the param should be set based on the mapping configuration (route_identifier_name and entity_identifier_name).

Is there an error in the configuration? Or is there a bug in the mapping?


Originally posted by @FrankGiesecke at https://github.com/zfcampus/zf-hal/issues/140

weierophinney commented 4 years ago

Further Information: The problem exists, if I my resources returns an paginator object. So the "magic" of zf-hal try's to generate the links for every entity in the collection based on the mapping_data configuration. But it doesn't set the parameters required by the route.


Originally posted by @FrankGiesecke at https://github.com/zfcampus/zf-hal/issues/140#issuecomment-202294716

weierophinney commented 4 years ago

As far as I know the real bug is that the RouteMatch is not set in the URL helper. Workaround:

    public function onBootstrap(MvcEvent $event)
    {
        $eventManager = $event->getApplication()->getEventManager();
        $eventManager->attach(MvcEvent::EVENT_ROUTE, [$this, 'onSetRouteMatch'], -1);
    }

    public function onSetRouteMatch(MvcEvent $event)
    {
        $viewHelpers = $event->getApplication()->getServiceManager()->get('ViewHelperManager');
        $viewHelpers->get('url')->setRouteMatch($event->getRouteMatch());
    }

This is because the route params are reused but there is not route match available because it's not set in the url helper.


Originally posted by @waltertamboer at https://github.com/zfcampus/zf-hal/issues/140#issuecomment-375599533