Skip to content
This repository has been archived by the owner on Jan 21, 2020. It is now read-only.

Dynamic route params for collections using the metadata_map #140

Open
FrankGiesecke opened this issue Mar 22, 2016 · 3 comments
Open

Dynamic route params for collections using the metadata_map #140

FrankGiesecke opened this issue Mar 22, 2016 · 3 comments

Comments

@FrankGiesecke
Copy link

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?

@FrankGiesecke
Copy link
Author

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.

@waltertamboer
Copy link

waltertamboer commented Mar 23, 2018

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.

@weierophinney
Copy link
Member

This repository has been closed and moved to laminas-api-tools/api-tools-hal; a new issue has been opened at laminas-api-tools/api-tools-hal#11.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants