Skip to content
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

Use JsonApi in routes to register adapters #50

Conversation

pedroluislopez
Copy link

This feature allow register adapters in the routes PHP file when you define JSON API resources. This feature avoid use a configuration file for register adapters and/or Eloquent adapter mappings. Example:

Route::group([
    'middleware' => ['api-v1'],
    'namespace' => 'Api',
    'prefix' => 'api/v1',
    'as' => 'api-v1::'
], function () {
    JsonApi::eloquentAdapter('comments', 'App\Comment');
    JsonApi::eloquentAdapter('people', 'App\Person');
    JsonApi::eloquentAdapter('posts', 'App\Post');
    JsonApi::adapter('sites', 'App\JsonApi\Sites\Adapter');
});

Copy link
Contributor

@jstoone jstoone left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just some nitpicking, hope you don't mind. :)

Not sure if @lindyhopchris automates this, but if not, we might as well help out. 👍

* @param string $model
* @param string $keyName
*/
public function addResource($resourceType, $model, $keyName = NULL)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use lowercase null in default param value

public function addResource($resourceType, $model, $keyName = null)

*/
public function addResource($resourceType, $model, $keyName = NULL)
{
$this->map[$resourceType] = $model;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add line between this statement and if-statement.

$this->map[$resourceType] = $model;

if (! is_null($keyName)) {
    $this->keyNames[$resourceType] = $keyName;
}

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also note the extra space between if and the start paran (.

$adapterObject = $this->container->make($adapter);

// Check if adapter recognises resource type
if(! $adapterObject->recognises($resourceType)) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add space between if and the start paran (:

if (! $adapterObject->recognises($resourceType)) {

* @param array $options
* @return ResourceRegistrar
*/
public function eloquentAdapter($resourceType, $model, $keyName = NULL, $controller = null, array $options = [])
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Make the default null parameter lowercase:

public function eloquentAdapter($resourceType, $model, $keyName = null, $controller = null, array $options = [])

@pedroluislopez
Copy link
Author

Done :)

@jstoone
Copy link
Contributor

jstoone commented Mar 1, 2017

Nice @pedroluislopez! 💯

@lindyhopchris
Copy link
Member

Sorry about this, but I don't think this will work if you cache your routes. Have you tried it with cached routes?

Also, I'm not sure it solves any problem. You say that it avoids putting stuff in configuration, but the config is there for a reason.

@pedroluislopez
Copy link
Author

Effectively it doesn't work with cache: routes.

About the purpose of this PR:

The modern frameworks do implement automatic features in order to avoid complex configuration files that are modified, usually, by several developers. For example, we work in the same project five developers.

Laravel has this philosophy. In fact, you use this philosophy in two aspects:

  1. You don't need to define in which folder the controller are. You define a namespace in routes.php for controllers and the framework search the controllers in the folder app/Controllers/{namespace}/.... The name also is calculate in CloudCreativity\LaravelJsonApi\Routing\ResourceRegistrar class.
    /**
     * @param $resourceType
     * @return string
     */
    protected function controllerFor($resourceType)
    {
        return Str::studly($resourceType) . 'Controller';
    }
  1. Also, you define a schema where you create Schemas and Adapters PHP files. It's used by php artisan commands. It's part of Laravel philosophy.
'generator' => [
        'namespace' => 'JsonApi',
        'by-resource' => true,
        'use-eloquent' => true,
    ],

However, effectively this solution doesn't work. If you like, I can offer other solutions based on the schema that you define for php artisan commands (always leaving a chance to be modify by developers in json-api configuration file).

I will close this PR, we will do other PR with other solutions and discuss about them if you like.

@lindyhopchris
Copy link
Member

Wherever we can move to automation, within the ways that Laravel 5 provides, I am supportive of it. The thing to remember is that this package is still in development (hence no 1.0 tag yet), and whatever your proposing needs to fit in with the general direction of where the package needs to head.

Please remember that in any automation you are proposing it must be capable of supporting multiple API definitions mapping to one set of models. I.e. the package has to support having multiple APIs that expose models in different ways.

I'd also strongly suggest you submit a proposal for the change via an issue before writing any code. That way we can discuss the idea before any code is written.

We are already working on changes to this package, which I've written up here so you can take a look:
#53

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

Successfully merging this pull request may close these issues.

3 participants