-
Notifications
You must be signed in to change notification settings - Fork 109
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
Question: how to create dynamic or reusable Adapters and Schemas? #203
Comments
Hi, All the classes are created via the Laravel container, so it is possible to set this up using bindings. E.g. when looking for an adapter this package will resolve public function register()
{
$this->app->bind('App\JsonApi\Posts\Adapter', MyAdapter::class);
$this->app->bind('App\JsonApi\Comments\Adapter', MyAdapter::class);
} Or you could use functions to customise your generic class: public function register()
{
$this->app->bind('App\JsonApi\Posts\Adapter', function () {
return new MyAdapter('posts');
});
} The same works for schemas, validators etc. Note that the binding name will depend on the namespace you have configured the JSON API with - i.e. in the examples I'm assuming you're using the default Let me know if you have any other questions! |
Actually not sure this will work at the mo, although this is how I intended it to work, so I might need to make a code change. Note for myself: currently only checking if the class exists, need to also check if there is a container binding here: |
Also, Adapter has constructor with 'new Model' statement. Maybe make some AdapterModelResolver or something like that. |
If I implement the above container bindings support, you can inject the model through the binding: public function register()
{
$this->app->bind('App\JsonApi\Posts\Adapter', function () {
return new MyAdapter(new MyModel());
});
} |
@tekord could you give this a go to see if it works for your scenario before I tag? You can install by using: $ composer require cloudcreativity/laravel-json-api:dev-master |
It works! Thank you :) |
Not critical, just for your information. If not set 'resourceType' in Schema class or set it to null then exception raised:
In my case the resource type is setup dynamically in binding. As workaround I set some resource type, but it does not make sense in case of dynamic schema. |
@tekord you'll still need to add the PHP class to the list |
In terms of setting the |
Released in |
But If I bind a schema to a generic Schema with $this->app->bind('App\JsonApi\Posts\Schema', function () {
return new GenericSchema(new \App\Post(), 'posts');
}); And I have the generic Schema like : class Schema extends SchemaProvider
{
/**
* @var string
*/
protected $resourceType;
/**
* Adapter constructor.
*
* @param StandardStrategy $paging
*/
public function __construct(SchemaFactoryInterface $factory, string $resourceType)
{
parent::__construct($factory);
$this->resourceType = $resourceType;
}
// ...
} I get I know I should not use |
Your schema binding should be:
|
Yes it works. Thank you very much !! |
I have 10 models with completelly the same structure (Dictionary Entries like SkillEntry, ExperienceEntry, etc, they use different tables, but models are extended from single class) and 1 model which referencing to some of these dictionary models. I don't want to create 10 folders with Adapter and Schema, too many duplicate code. Is there any way to make schema and adapter dynamically? Or use the same Adapter and Schema but change their resource types dynamically? For instance:
User model has SkillEntry and ExperienceEntry relations (and another 8 entry relations). SkillEntry's resource type is 'dictionaries_skill-entries', ExperienceEntry's resource type is 'dictionaries_experience-entries'. These relations are available as normal relation of User resource by URL like this: /api/v1/users/42/relationships/skill-entry.
UPD. DictionaryEntry models has NO any relations. But if they were I also interesting in how to deal with it.
Thanks.
The text was updated successfully, but these errors were encountered: