tobyzerner / json-api-php

JSON-API (http://jsonapi.org) responses in PHP.
MIT License
436 stars 79 forks source link

Better Laravel Eloquent Support #66

Closed vinkla closed 9 years ago

vinkla commented 9 years ago

Wouldn't it be nice if we could typehint what model we're throwing to the serializers?

public function getAttributes(User $user) {
    //
}

Would this be possible to implement and would it take long time to do?

tobyzerner commented 9 years ago

Not really possible because the serializers must adhere to an interface, so they can't stray from that with type hints.

Instead, you can do something like this:

public function getAttributes($user) {
    if (!($user instanceof User)) {
        throw new InvalidArgumentException;
    }
}
vinkla commented 9 years ago

Okay, to bad.

vinkla commented 8 years ago

@tobscure do you have any examples or tips for integrating the new version with Laravel?

tobyzerner commented 8 years ago

See these methods for an example implementation

vinkla commented 8 years ago

Thanks! How would go about binding the document to Laravel request?

tobyzerner commented 8 years ago

The Document object implements the JsonSerializable interface so you should be able to pass it right into the response helper, e.g.:

return response($document)
    ->header('Content-Type', 'application/vnd.api+json');
vinkla commented 8 years ago

Awesome, thanks for helping out!