Coder Social home page Coder Social logo

luis4609 / symfony5-rest-api Goto Github PK

View Code? Open in Web Editor NEW

This project forked from demartis/symfony5-rest-api

0.0 0.0 0.0 437 KB

API REST skeleton with Symfony 5, following best practices and implementing clean architecture.

License: MIT License

PHP 89.56% Dockerfile 1.54% Gherkin 3.51% Shell 4.60% Twig 0.79%

symfony5-rest-api's Introduction

Symfony 5 REST API skeleton

Symfony 5 + FOSRestBundle + JSON Standard responses + working example

Travis (.org) GitHub last commit GitHub repo size in bytes GitHub language count GitHub top language PHP from Travis config GitHub FOSSA Status

Table of Contents

About

Symfony 5 skeleton to build REST APIs, inclusive of:

  • FOSRestBundle (friendsofsymfony/rest-bundle) to simplify the entire process
  • Hateoas Bundle (willdurand/hateoas-bundle) that specifies relation types for Web links
  • Doctrine

This project is compliant with:

JTTP Coherent output formats

JTTP is the default protocol

General JTTP output format:

{
    "status": "success|error",
    "code": "HTTP status code",
    "message": "HTTP status message",
    "data|error": {
        "your data": "data or error field only in case of success or error"
    }
}

Example - GET resource: GET /v1/books/1

{
    "status": "success",
    "code": 200,
    "message": "OK",
    "data": {
        "id": 1,
        "title": "PHP & MySQL Novice to Ninja",
        "_links": {
            "self": {
                "href": "/v1/books/1"
            }
        }
    }
}

Example - GET collection: GET /v1/books

{
    "status": "success",
    "code": 200,
    "message": "OK",
    "data": [
        {
            "id": 1,
            "title": "PHP & MySQL Novice to Ninja",
            "_links": {
                "self": {
                    "href": "/v1/books/1"
                }
            }
        },
        {
            "id": 2,
            "title": "Head First PHP & MySQL",
            "pages": 812,
            "_links": {
                "self": {
                    "href": "/v1/books/2"
                }
            }
        }
    ]
}

Example - POST resource: POST /v1/books

JSON (any other field will be ignored):

{
    "data": {
        "title": "New Book about PHP",
        "pages": 123
    }
}

Response:

{
    "status": "success",
    "code": 200,
    "message": "OK",
    "data": {
        "id": 3,
        "title": "New Book about PHP",
        "pages": 123,
        "_links": {
            "self": {
                "href": "/v1/books/12"
            }
        }
    }
}

Example - PUT resource: PUT /v1/books/1

JSON (any other field will be ignored):

{
    "data": {
        "title": "Edit title",
        "pages": 1000
    }
}

Response:

{
    "status": "success",
    "code": 200,
    "message": "OK",
    "data": {
        "id": 1,
        "title": "Edit title",
        "pages": 1000,
        "_links": {
            "self": {
                "href": "/v1/books/1"
            }
        }
    }
}

Example - error: Resource not found: GET /v1/books/123123

{
    "status": "error",
    "code": 404,
    "message": "Not Found",
    "error": {
        "details": "Resource 123123 not found"
    }
}

Example - error: Route not found: GET /wrongroute123

{
    "status": "error",
    "code": 404,
    "message": "Not Found",
    "error": {
        "details": "No route found for \"GET /wrongroute123\""
    }
}

Example - 500 Internal Server Error

{
    "status": "error",
    "code": 500,
    "message": "Internal Server Error",
    "error": {
        "details": "Notice: Undefined variable: view"
    }
}

Example - form error - POST /v1/books

{
    "data": {
        "pages": 123
    }
}

Response:

{
    "status": "error",
    "code": 400,
    "message": "Bad Request",
    "error": {
        "form": {
            "title": "This value should not be blank."
        }
    }
}

Getting Started

These instructions will get you a copy of the project up and running on your local machine for development and testing purposes.

Prerequisites

What things you need to install the software and how to install them.

Installing

git clone https://github.com/demartis/symfony5-rest-api/
cd symfony5-rest-api
cp .env.dist .env
## edit .env if needed
composer install
symfony server:start

Installing (alternative with Docker)

git clone https://github.com/demartis/symfony5-rest-api/
cd symfony5-rest-api
cp .env.dist .env
## edit .env if needed
docker-compose build
docker-compose up

Running the example

Install database

php bin/console doctrine:database:create
php bin/console doctrine:migrations:migrate

Get with Curl

curl -H 'content-type: application/json' -v -X GET http://127.0.0.1:8000/v1/books
curl -H 'content-type: application/json' -v -X GET http://127.0.0.1:8000/v1/books/2 

Using it as skeleton

Add notes about how to use the system.

Contributing

  1. Fork it (https://github.com/demartis/symfony5-rest-api/fork)
  2. Create your feature branch (git checkout -b feature/fooBar)
  3. Commit your changes (git commit -am 'Add some fooBar')
  4. Push to the branch (git push origin feature/fooBar)
  5. Create a new Pull Request

License

FOSSA Status

symfony5-rest-api's People

Contributors

demartis avatar dependabot[bot] avatar fossabot 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.