Comments (9)
Closing this as it is effectively solved via #196
I.e. in 1.0.0-rc.1
you'll be able to add custom actions for a resource, so you could do this to add a me
route for the users
resource:
JsonApi::register('default')->withNamespace('Api')->routes(function ($api) {
$api->resource('users')->controller()->routes(function ($posts) {
$posts->get('me', 'current');
});
});
Which would mean GET /api/users/me
would be handled by the Api\UsersController@current
action.
from laravel-json-api.
Hi @lindyhopchris
I think it can be easily implemented by commenting
throw new RuntimeException('No matching resource type from the current route name.');
in RequestInterpreter class.
So after this we can add any route we want and place custom actions in same controller.
With this solution I added /users/me route and map it to method in UsersController class. This class extend EloquentController and serve all json-api stuff.
Is it good solutions? Or not? What side effects can we get with such solution?
Thanks in advance
from laravel-json-api.
or maybe @jstoone have thoughts about this
from laravel-json-api.
If that works at the moment then I suppose it might be acceptable at the moment.
I think the more permanent solution going forward would be to use the options when registering the JSON-API resource with the route JsonApi::resource('users')
to pass in something that registers me
as an extra route on the same controller.
That should be fairly straight-forward to do, but we'd then need to follow through what happens with all the parsing of the request to check that it doesn't start throwing exceptions elsewhere. The main problem is that because the endpoint is not defined in the spec, it's not really possible to know how to validate the request content for a POST
or PUT
request.
from laravel-json-api.
This sounds like a really neat feature!
The first thought that hit me, was that these singular resources sound very much like an alias to me. And since the singular resources are ideally going to be quite few, I'd actually suggest an implementation that could fit in either the RouteServiceProvider
or route file without being to intrusive, which would allow us to achieve a resource alias api like:
// The array key is the JSON:API `resourceType`
// The array value is the ID identifier for the resource
// The last string argument is the final alias to get the resource
JsonApi::resourceAlias(
['users' => auth()->id()]
'/users/me'
);
NB: for simplicity sake lets assume the currently authenticated user has an ID of 5
Then we can logically keep it simple:
- Register alias within a container
- When request arrives we can check if the resource type has an alias
- In this case the resource type is
users
- In this case the resource type is
- If we have an alias, we can transform the current aliased request to the specific path
- In the example above we would transform from
/users/me
to/users/5
- In the example above we would transform from
- Now we can continue processing as if the request was
/users/5
from the start
I might not have understood or not think the solution through, so I hope it makes sense @dmitry-php @lindyhopchris. 😄
from laravel-json-api.
@lindyhopchris I think I'll start taking a look at this, and then we can iterate if it does not seem to fit the style you have in mind. 🙂
All of a sudden laravel-json-api got a lot of activity. Nice! Let the work begin! 😅
from laravel-json-api.
@jstoone sorry, meant to reply earlier. The alias won't work because you won't have the authenticated user id at the time that you are registering routes.
from laravel-json-api.
@lindyhopchris hmm maybe we could enable the resource value to be a closure? In that way we could evaluate the value when transforming the aliased request?
from laravel-json-api.
Closures won't be compatible with route:cache
.
I think it's some sort of config in the options that can be passed to JsonApi::resource()
that registers these additional routes to a specific method on the controller. But not sure right at this moment what that might be.
Also I'm planning to make some changes to the routing stuff so that the current $hasMany
and $hasOne
properties on the Request
object actually get used when registering the routes. So it might be better to wait until I've made that change before thinking about how we also do singular resources? Or change both at once?
from laravel-json-api.
Related Issues (20)
- Unable to create json api resource with attribute name "type" HOT 3
- Self-relation doesn't include model fields HOT 5
- Arguement 3 passed to controller must be an instance of model, string given
- Support for multiple operations in a single request HOT 2
- use Errors Handling in Laravel 8 HOT 1
- Why does json-api trimming spaces in my attributes when updating? HOT 3
- filter slug and underscore HOT 1
- Multiple primary keys - Mysql Error HOT 1
- Sparse fieldsets not working with included relationships HOT 3
- How to attach a middleware to a specific resource relationship? HOT 3
- How I can filter included resources by their fields HOT 8
- Migrate from cloudcreativity/laravel-json-api to laravel-json-api/laravel HOT 15
- How to stop this library from making asterix queries? HOT 1
- Make a request to one endpoint from another controller HOT 5
- Insert on duplicate key upd when inserting a record? HOT 6
- Handle HasMany relationship with extra field. HOT 2
- [5.x] Missing meta HOT 8
- bug: service provider boot throws "No application encryption key has been specified." HOT 1
- Error in tests HOT 1
- getResourceLinks function HOT 3
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from laravel-json-api.