GithubHelp home page GithubHelp logo

ekonoval / doctrine-rest Goto Github PK

View Code? Open in Web Editor NEW

This project forked from r3volut1oner/doctrine-rest

0.0 0.0 0.0 270 KB

Rest API for Doctrine ORM

License: MIT License

PHP 99.84% Dockerfile 0.16%

doctrine-rest's Introduction

doctrine-rest

Build Status Scrutinizer Code Quality Scrutinizer Code Coverage

Framework agnostic, library provides basic tools for implementation of JSON API over Doctrine library

Using by default symfony/http-foundation for requests/responses and league/fractal for Rest response build.

Install

Add composer package to your project

composer require pz/doctrine-rest

Usage

Package provides different actions for data manipulation and formatting.

Create entity and fractal trasformer for the entity.

// Entity class to work with
$entityClass = 'User';
$entityTransformer = new EntityTransformer();

If you want to use JSON API please implement JsonApiResource on your doctrine entity and add next header to request:

Accept: application/vnd.api+json

Change entity repository to RestRepository or create new one.

// Provide configured entity manager
$entityManager = getEntityManager()

// Repository that action will work with
$restRepository = new RestRepository($entityManager, $entityManager->getClassMetadata($entityClass));

Prepare RestRequest entity or implement RestRequestContract on your custom RestRequest implementation.

// Get http request from framework or init yourself
$httpRequest = Symfony\Component\HttpFoundation\Request::createFromGlobals();
$restRequest = new RestRequest($httpRequest);

Collection (Index) action

Route request GET http://localhost/api/{resourceKey}

$action = new CollectionAction($restRepository, $entityTransformer);

/** @var RestResponse|Symfony\Component\HttpFoundation\Response */
$response = $action->dispatch($restRequest);

Regular response

{
    'data': [
        { ...transformer data },
        { ...transformer data },
        { ...transformer data },
    ],
    'meta': [
        'pagination': { ... paginator data },
    ]
}

Json api response

{
    'data': [
        {
            'id': {entityId},
            'type': {etntityResourceKey},
            'attributes': { ...transformer data },
            'relationships': { ..transformer includes },
            'links': {
                'self': 'http://localhost/api/resourceKey/{entityId}
            }
        },
        ... Other entities
    ],
    'meta': [
        'pagination': { ... paginator data },
    ]
}

Item (Get) action

Route request GET http://localhost/api/{resourceKey}/{id}.

$action = new ItemAction($restRepository, $entityTransformer);

/** @var RestResponse|Symfony\Component\HttpFoundation\Response */
$response = $action->dispatch($restRequest);

Regular response

{
    'data': [
        'id': {id},
         { ...transformer data }
    ],
}

Json api response

{
    'data': {
        'id': {entityId},
        'type': {etntityResourceKey},
        'attributes': { ...transformer data },
        'relationships': { ..transformer includes },
        'links': {
            'self': 'http://localhost/api/resourceKey/{entityId}
        }
    },
}

Create action

Route request POST http://localhost/api/{resourceKey}.

$action = new CreateAction($restRepository, $entityTransformer);

/** @var RestResponse|Symfony\Component\HttpFoundation\Response */
$response = $action->dispatch($restRequest);

Regular response

{
    'data': [
        'id': {id},
         { ...transformer data }
    ],
}

Json api response

{
    'data': {
        'id': {entityId},
        'type': {etntityResourceKey},
        'attributes': { ...transformer data },
        'relationships': { ..transformer includes },
        'links': {
            'self': 'http://localhost/api/resourceKey/{entityId}
        }
    },
}

Update action

Route request PATCH http://localhost/api/{resourceKey}/{id}.

$action = new UpdateAction($restRepository, $entityTransformer);

/** @var RestResponse|Symfony\Component\HttpFoundation\Response */
$response = $action->dispatch($restRequest);

Regular response

{
    'data': [
        'id': {id},
         { ...transformer data }
    ],
}

Json api response

{
    'data': {
        'id': {entityId},
        'type': {etntityResourceKey},
        'attributes': { ...transformer data },
        'relationships': { ..transformer includes },
        'links': {
            'self': 'http://localhost/api/resourceKey/{entityId}
        }
    },
}

Delete action

Route request DELETE http://localhost/api/{resourceKey}/{id}.

$action = new DeleteAction($restRepository, $entityTransformer);

/** @var RestResponse|Symfony\Component\HttpFoundation\Response */
$response = $action->dispatch($restRequest);

Response

HTTP STATUS 204 NO CONTENT

Development

Generate doctrine migration diff

We using doctrine migrations for unit tests database schema.

php ./vendor/bin/doctrine-migrations migrations:diff

Run tests

docker-compose up -d
docker-compose run php phpunit

doctrine-rest's People

Contributors

r3volut1oner avatar jrean avatar ekonoval 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.