Giter Club home page Giter Club logo

demo-laravel-json-api's Introduction

Laravel JSON API Demo

This application demonstrates how to use the cloudcreativity/laravel-json-api package to create a JSON API compliant API. This is demonstrated using Eloquent models as the domain records that are serialized in the API, but the package is not Eloquent specific.

Setup

The application uses Homestead, so you'll need Vagrant installed on your local machine.

Once you've cloned this repository, change into the project folder then:

composer install
cp .env.example .env
php vendor/bin/homestead make
vagrant up

Remember you'll need to add an entry for homestead.app in your /etc/hosts file.

Once it is up and running, go to the following address in your browser:

http://homestead.app/api/v1/posts

Explanation

As an example, this API exposes a posts resource. To do this:

  1. The resource is registered with the router in app/Http/routes.php
  2. A controller exists: app/Http/Controllers/Api/PostsController. This is composed of units that each have a single concern, and can be found in the app/JsonApi/Posts folder:
  • An adapter class, that handles finding Post models either from a JSON API resource identifier or from a JSON API search request (filtering, pagination etc).
  • A hydrator, that contains the logic for transferring data from the client's request into the domain record (the Post model).
  • A schema that handles converting the Post model into its API representation.
  • A validators provider, that provides the rules for validating the HTTP content for a create or update request, as well as validating any query parameters.

Eloquent vs Not-Eloquent

This package can handle both Eloquent and non-Eloquent records. You get a lot more functionality out of the box if you are using Eloquent, but it's possible to integrate non-Eloquent records as needed.

This demo includes the following JSON-API resources:

Resource Record Eloquent?
comments App\Comment Yes
people App\People Yes
posts App\Post Yes
sites App\Site No
tags App\Tag Yes

To support our non-Eloquent records, we've created a custom schema and hydrator class in this application. These use traits that are included in the package to replicate the behaviour of the Eloquent schema/hydrator. The custom schema and hydrator are in the App\JsonApi namespace.

Tests

We're big on testing, and the cloudcreativity/laravel-json-api package comes with test helpers to make integration testing a JSON API a breeze. You can see this in action in the tests/Integration folder, where there's a test case for the posts resource.

To run the tests:

vagrant ssh
cd /vagrant
vendor/bin/phpunit

demo-laravel-json-api's People

Contributors

giantcrab avatar gregpeden avatar lindyhopchris avatar

Stargazers

 avatar

Watchers

 avatar  avatar

demo-laravel-json-api's Issues

Exception when running DELETE test

I'm currently getting this error when running the DELETE test (for logout). Need to figure out why this is happening and how to fix it.

+exception: CloudCreativity\JsonApi\Exceptions\InvalidJsonException {#702
      -jsonError: null
      -jsonErrorMessage: "JSON is not an object."
      -errors: Neomerx\JsonApi\Exceptions\ErrorCollection {#707
        -items: []
      }
      -httpCode: 400
      #message: "JSON API error"
      #code: 0
      #file: "/home/woohuiren/Programming/demo-laravel-json-api/vendor/cloudcreativity/json-api/src/Decoders/Helpers/DecodesJson.php"
      #line: 48
      trace: {
        /home/woohuiren/Programming/demo-laravel-json-api/vendor/cloudcreativity/json-api/src/Decoders/Helpers/DecodesJson.php:48: { …3}
        /home/woohuiren/Programming/demo-laravel-json-api/vendor/cloudcreativity/json-api/src/Decoders/DocumentDecoder.php:44: { …4}
        /home/woohuiren/Programming/demo-laravel-json-api/vendor/cloudcreativity/json-api/src/Http/Requests/RequestFactory.php:116: { …4}
        /home/woohuiren/Programming/demo-laravel-json-api/vendor/cloudcreativity/json-api/src/Http/Requests/RequestFactory.php:82: { …4}
        /home/woohuiren/Programming/demo-laravel-json-api/vendor/cloudcreativity/json-api/src/Factories/Factory.php:102: { …4}
        /home/woohuiren/Programming/demo-laravel-json-api/vendor/cloudcreativity/laravel-json-api/src/Http/Middleware/BootJsonApi.php:125: { …4}
        /home/woohuiren/Programming/demo-laravel-json-api/vendor/cloudcreativity/laravel-json-api/src/Http/Middleware/BootJsonApi.php:85: { …4}
        /home/woohuiren/Programming/demo-laravel-json-api/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php:148: { …4}
        /home/woohuiren/Programming/demo-laravel-json-api/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php:53: { …4}
        /home/woohuiren/Programming/demo-laravel-json-api/vendor/laravel/framework/src/Illuminate/Routing/Middleware/SubstituteBindings.php:41: { …4}
        /home/woohuiren/Programming/demo-laravel-json-api/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php:148: { …4}
        /home/woohuiren/Programming/demo-laravel-json-api/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php:53: { …4}
        /home/woohuiren/Programming/demo-laravel-json-api/vendor/laravel/framework/src/Illuminate/Auth/Middleware/Authenticate.php:43: { …4}
        /home/woohuiren/Programming/demo-laravel-json-api/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php:148: { …4}
        /home/woohuiren/Programming/demo-laravel-json-api/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php:53: { …4}
        /home/woohuiren/Programming/demo-laravel-json-api/vendor/laravel/framework/src/Illuminate/Routing/Middleware/ThrottleRequests.php:49: { …4}
        /home/woohuiren/Programming/demo-laravel-json-api/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php:148: { …4}
        /home/woohuiren/Programming/demo-laravel-json-api/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php:53: { …4}
        /home/woohuiren/Programming/demo-laravel-json-api/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php:102: { …4}
        /home/woohuiren/Programming/demo-laravel-json-api/vendor/laravel/framework/src/Illuminate/Routing/Router.php:576: { …4}
        /home/woohuiren/Programming/demo-laravel-json-api/vendor/laravel/framework/src/Illuminate/Routing/Router.php:535: { …4}
        /home/woohuiren/Programming/demo-laravel-json-api/vendor/laravel/framework/src/Illuminate/Routing/Router.php:513: { …4}
        /home/woohuiren/Programming/demo-laravel-json-api/vendor/laravel/framework/src/Illuminate/Foundation/Http/Kernel.php:174: { …4}
        /home/woohuiren/Programming/demo-laravel-json-api/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php:30: { …4}
        /home/woohuiren/Programming/demo-laravel-json-api/vendor/laravel/framework/src/Illuminate/Foundation/Http/Middleware/CheckForMaintenanceMode.php:46: { …4}
        /home/woohuiren/Programming/demo-laravel-json-api/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php:148: { …4}
        /home/woohuiren/Programming/demo-laravel-json-api/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php:53: { …4}
        /home/woohuiren/Programming/demo-laravel-json-api/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php:102: { …4}
        /home/woohuiren/Programming/demo-laravel-json-api/vendor/laravel/framework/src/Illuminate/Foundation/Http/Kernel.php:149: { …4}
        /home/woohuiren/Programming/demo-laravel-json-api/vendor/laravel/framework/src/Illuminate/Foundation/Http/Kernel.php:116: { …4}
        /home/woohuiren/Programming/demo-laravel-json-api/vendor/laravel/browser-kit-testing/src/Concerns/MakesHttpRequests.php:582: { …4}
        /home/woohuiren/Programming/demo-laravel-json-api/vendor/laravel/browser-kit-testing/src/Concerns/MakesHttpRequests.php:75: { …4}
        /home/woohuiren/Programming/demo-laravel-json-api/vendor/cloudcreativity/laravel-json-api/src/Testing/MakesJsonApiRequests.php:64: { …4}
        /home/woohuiren/Programming/demo-laravel-json-api/tests/Integration/TokensTest.php:66: { …4}
        App\Tests\Integration\TokensTest->testDelete(): {}
        /home/woohuiren/Programming/demo-laravel-json-api/vendor/phpunit/phpunit/src/Framework/TestCase.php:1054: { …4}
        /home/woohuiren/Programming/demo-laravel-json-api/vendor/phpunit/phpunit/src/Framework/TestCase.php:905: { …3}
        /home/woohuiren/Programming/demo-laravel-json-api/vendor/phpunit/phpunit/src/Framework/TestResult.php:686: { …3}
        /home/woohuiren/Programming/demo-laravel-json-api/vendor/phpunit/phpunit/src/Framework/TestCase.php:860: { …4}
        /home/woohuiren/Programming/demo-laravel-json-api/vendor/phpunit/phpunit/src/Framework/TestSuite.php:722: { …4}
        /home/woohuiren/Programming/demo-laravel-json-api/vendor/phpunit/phpunit/src/TextUI/TestRunner.php:517: { …4}
        /home/woohuiren/Programming/demo-laravel-json-api/vendor/phpunit/phpunit/src/TextUI/Command.php:186: { …4}
        /home/woohuiren/Programming/demo-laravel-json-api/vendor/phpunit/phpunit/src/TextUI/Command.php:116: { …4}
        /home/woohuiren/Programming/demo-laravel-json-api/vendor/phpunit/phpunit/phpunit:52: { …3}
      }
}

Steps to reproduce:

  • Run vendor/bin/phpunit tests/Integration/TokensTest.php

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.