woohoolabs / yin

The efficient and elegant JSON:API 1.1 server library for PHP
MIT License
239 stars 38 forks source link

Laravel 5 Integration help #71

Closed scryba closed 6 years ago

scryba commented 6 years ago

Hello I have finally integrated Yin into laravel 5.6 with dingo API But stuck here.

Below is my code.

public function show()
{

    // Find the current route
    $exceptionFactory = new DefaultExceptionFactory();
    $deserializer = new JsonDeserializer();
    $request = new Request(ServerRequestFactory::fromGlobals(), $exceptionFactory, $deserializer);
    // Invoking the current action
    $jsonApi = new JsonApi($request, new Response(), $exceptionFactory);

    $id = $jsonApi->getRequest()->getAttribute("id");

    ////country
    $country = CountryRepository::getCountry($id);

    // Instantiating a book document
    $document = new CountryDocument(
        new CountryResourceTransformer()
    );
    // Responding with "200 Ok" status code along with the book document
    return $jsonApi->respond()->ok($document, $country);

} 

$id = $jsonApi->getRequest()->getAttribute("id"); returns null

my url is of the form http://api.example.com/countries/281615

Are mine implementing this the right way or do i have to use laravel/Dingo Request class Dingo\Api\Http\Request

my route

$api = app('Dingo\Api\Routing\Router');
$api->version('v1', function ($api) {
    $api->get('countries/{country}', 'App\Api\Controllers\CountryController@show');
}

Works fine if manually set the id for instance on my user class

$user = UserRepository::getUser(1233);

    // Instantiating a book document
    $document = new UserDocument(
        new UserResourceTransformer(
            new CountryResourceTransformer()
        )
    );
    // Responding with "200 Ok" status code along with the book document
    return $jsonApi->respond()->ok($document, $user);

using the request url

http://api.example.com/users/11122?include=country&limit=1

I get the response

{
"jsonapi": {
    "version": "1.0"
},
"links": {
    "self": "/?path=/user/1233"
},
"data": {
    "type": "users",
    "id": "1233",
    "links": {
        "self": "/?path=/users/1233"
    },
    "attributes": {
        "title": "Prof.",
        "firstname": "Theo",
        "lastname": "Parisian",
        "othernames": "Sr.",
        "email": "cstracke@example.com",

    },
    "relationships": {
        "country": {
            "links": {
                "self": "/?path=/users/1233/relationships/country"
            },
            "data": {
                "type": "countries",
                "id": "560515"
            }
        }
    }
},
"included": [
    {
        "type": "countries",
        "id": "560515",
        "links": {
            "self": "/?path=/countries/560515"
        },
        "attributes": {
            "id": 560515,
            "long_name": "Armenia",
            "short_name": "Sudan",

        }
    }
]
}

Thanks just need some guidance to complete this.

gfemorris commented 6 years ago

Hi,

you should use the PSR-Bridge to have the support of PSR7 requests Laravel: https://laravel.com/docs/5.6/requests#psr7-requests

After integration you can just type hint the request in your action: public function show(ServerRequestInterface $request)

With the PSR7 request already available it should be easy to create the jsonapi object: $jsonApi = new JsonApi(new \WoohooLabs\Yin\JsonApi\Request\Request($request, $exceptionFactory), new \Zend\Diactoros\Response(), $exceptionFactory);

The id should be available through the routing: Route::get('example/{id}', 'ExampleController@show')->name('example.show');

public function show(ServerRequestInterface $request, $id)

Your problems here are not related to woohoolabs/yin. They are related to laravel. I think you should ask those question in the laravel community or on stackoverflow.

scryba commented 6 years ago

Thanks @gfemorris very very helpful.

kocsismate commented 6 years ago

@gfemorris Thank you very much for the detailed answer :) I am not familiar with Laravel at all so it was really helpful for me too!

kocsismate commented 6 years ago

I'll close this now. I hope that all your problems have been solved in connection with this issue.