Coder Social home page Coder Social logo

skinnyshugo / demo-laravel-json-api Goto Github PK

View Code? Open in Web Editor NEW

This project forked from cloudcreativity/demo-laravel-json-api

0.0 0.0 0.0 1.78 MB

Demo of JSON API integration with a Laravel Application

PHP 74.10% JavaScript 0.83% Shell 0.32% Vue 15.14% Blade 9.60%

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 to see the JSON endpoints:

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

To access the web interface:

http://homestead.app

If you use the Vagrant hosts updater plugin, the hostname may be demo-laravel-json-api or similar.

Authentication

Any write requests require an authenticated user. We've installed Laravel Passport for API authentication. You will need to use Personal Access Tokens and the Vagrant provisioning runs the Passport installation command.

To create a token, go to the web interface and login (the username and password fields are completed with credentials that will sign you in successfully). You'll then see the Passport Person Access Token component which you can use to issue tokens.

Once you have a token, send a request as follows, replacing the <api_token> with your token.

POST http://homestead.app/api/v1/posts
Accept: application/vnd.api+json
Content-Type: application/vnd.api+json
Authorization: Bearer <api_token>

{
    "data": {
        "type": "posts",
        "attributes": {
            "slug": "hello-world",
            "title": "Hello World",
            "content": "..."
        }
    }
}

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
posts App\Post Yes
sites App\Site No
tags App\Tag Yes
users App\User Yes

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:

vendor/bin/phpunit

demo-laravel-json-api's People

Contributors

dependabot[bot] avatar gregpeden avatar lindyhopchris avatar martynling avatar neonbunny avatar

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.