Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
weierophinney opened this issue Dec 31, 2019 · 3 comments
Open

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

weierophinney opened this issue Dec 31, 2019 · 3 comments

Comments

@weierophinney
Copy link
Contributor

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.


Originally posted by @danbroooks at zfcampus/zf-hal#136

@weierophinney
Copy link
Contributor Author

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.


Originally posted by @TomHAnderson at zfcampus/zf-hal#136 (comment)

@weierophinney
Copy link
Contributor 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)?


Originally posted by @danbroooks at zfcampus/zf-hal#136 (comment)

@weierophinney
Copy link
Contributor Author

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));

Originally posted by @Wilt at zfcampus/zf-hal#136 (comment)

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

No branches or pull requests

1 participant