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

Is it possible to hint at the HTTP method with this library? #136

Open
danbroooks opened this issue Feb 8, 2016 · 4 comments
Open

Is it possible to hint at the HTTP method with this library? #136

danbroooks opened this issue Feb 8, 2016 · 4 comments

Comments

@danbroooks
Copy link

I'm not sure if I've managed this before or not, thought I had but can't seem to find the configuration... Is it possible to return a HTTP method inside a link? I want to achieve something similar to Paypal's HATEOAS implementation, where the HTTP method is returned along with a link.

My metadata map currently looks like this:

    'Users\\V1\\Rest\\User\\UserEntity' => array(
        'entity_identifier_name' => 'id',
        'route_name' => 'users.rest.user',
        'route_identifier_name' => 'user_id',
        'hydrator' => 'Zend\\Hydrator\\ClassMethods',
        'links' => array(
            'role' => array(
                'rel' => 'create-role',
                'route' => array(
                    'name'    => 'users.rest.roles',
                    'options' => array(
                        'method' => 'GET'
                    ),
                ),
            )
        ),
    ),

Though it is currently unsuccessful, I've tried 'params' as well but to no avail.

@TomHAnderson
Copy link
Contributor

I think you're looking for this: https://github.com/zfcampus/zf-hal/blob/master/src/Link/Link.php#L133

I don't know why href is ignored, but arbitrary attributes does seem like what you need.

@danbroooks
Copy link
Author

That's perfect. Is it worth making a PR to add this into the metadata_map part of the readme?

When using this in tandem with the event system (I want to add my own HATEOAS links based on certain conditions), it seems to break the dynamic creation of routes... When I have this code in my module bootstrap:

public function onBootstrap(MvcEvent $e)
{
    /** @var \Zend\Mvc\Application $app */
    $app = $e->getTarget();

    $view = $app->getServiceManager()->get('ViewHelperManager');

    $hal = $view->get('Hal');

    $hal->getEventManager()
        ->attach('renderEntity', array($this, 'onRenderEntity'));
}

public function onRenderEntity($e)
{
    $entity = $e->getParam('entity');
}

These routes seem to lose their dynamic parameter generation (for the above rule I get Missing parameter \"user_id\" for example). I was hoping to use a combination of these hard coded route rules, and dynamic ones built depending on resource conditions.

FYI I am working in Apigility. Is this the correct way in Apigility to create dynamic HATEOAS links (there doesn't seem to be a way to do it from the Resource or Entity)?

@Wilt
Copy link
Contributor

Wilt commented Feb 9, 2016

We do it exactly like that in our project. We have a listener and then we add the methods:

use Zend\Http\Request;

//....
$halLinks = $entity->getLinks();
$self = $halLinks->get('self');
$methods = array( Request::METHOD_GET, Request::METHOD_POST );
$self->setProps(array('methods' => $methods));

@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#13.

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

4 participants