Skip to content

Custom resource routes #63

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

Closed
flofloflo opened this issue May 30, 2017 · 8 comments
Closed

Custom resource routes #63

flofloflo opened this issue May 30, 2017 · 8 comments
Milestone

Comments

@flofloflo
Copy link

Hello, first of all thanks for sharing this great project with the public!

I'm trying to set up a custom resource route but can't find a way to do it properly. The routing documentation only shows the possibility to include or exclude controller actions of the predefined pool (index, create, read, etc.).

Is it possible to create a custom route which points to a custom controller action inside of the JsonApi::api( ... ) block? Or is this the wrong place for custom actions and the route should be defined "by hand" as classic Route::get( ... )?

@lindyhopchris
Copy link
Member

Hi! Glad you like the package. And thanks for raising this, as this is something I'd like to get settled for v1.0 so your ideas would be useful.

At the moment you need to use the traditional Laravel router methods, e.g.:

JsonApi::api('default', ['as' => 'api.'], function ($api, $router) {
    $api->resource('posts');
    $router->get('posts/my-action', 'PostsController@myAction');
    $api->resource('comments');
});

This however won't have the authorization or validation middleware that is applied using $api->resource('posts'); The reason being is that both bits of middleware use a request interpreter to determine how they need to authorize/validate the request based on the JSON API spec as to what route is being called. However because your controller action is outside of the spec, the middleware won't necessarily interpret it correctly.

Let me know how you get on. I'm not totally sure how to integrate custom actions as technically they are outside the spec, so it's a bit difficult for me to reason about how to handle them within the package.

@lindyhopchris
Copy link
Member

Actually, looking at the code I'm not 100% sure this will work: building the JSON API request object might get in trouble as it uses the request interpreter for that.

If it doesn't work, you'll have to define it outside the api method, but this will cause problems in sending a JSON API response.

I might have to look into this a bit more.

@flofloflo
Copy link
Author

Thanks for the fast reply!

In the meantime I have found a way to replace the need for a custom route with a simple filter inside the adapter, so please don't put too much effort into this issue :-)

Adding the custom route inside the api method indeed causes an exception: CloudCreativity\JsonApi\Exceptions\RuntimeException: No matching resource type from the current route name..
Calling it outsitde gives CloudCreativity\JsonApi\Exceptions\RuntimeException: No active API. The JSON API middleware has not been run..
Maybe the easiest or most flexible solution would be to provide a way to create a custom JSON API response without having to run the middleware.

@lindyhopchris
Copy link
Member

yeah, that's along the lines of what I was thinking too. I'll leave this issue open so that it doesn't fall off the radar!

@tooshay
Copy link

tooshay commented Nov 1, 2017

@flofloflo Could you elaborate on how exactly you overcame this issue using a filter? I'm stuck in the exact place you were...

@flofloflo
Copy link
Author

I'm sorry, I don't exactly recall what my problem was back in May.

But I remember that after some thinking I realized that I didn't need a separate controller method but rather a filter for my database query. Filtering can easily be done through the adapter: -> See the example from the demo

If you need a plain JSON API response without the magic of this package, you could also try Fractal with the JsonApiSerializer and build the response manually. (That's what I did in an other case)

@lindyhopchris
Copy link
Member

@tooshay hey! if you let me know what you're stuck on I'd be happy to help

@lindyhopchris lindyhopchris added this to the 1.0.0 milestone Jan 19, 2019
@lindyhopchris
Copy link
Member

Closing this in favour of #196

A solution to custom routes will be available in 1.0.0-rc.1

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

3 participants