2017 © Pedro Peláez
 

library api-server

A framework for rapid REST API development.

image

fuzz/api-server

A framework for rapid REST API development.

  • Monday, May 14, 2018
  • by Fuzzpro
  • Repository
  • 10 Watchers
  • 6 Stars
  • 7,988 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 3 Forks
  • 2 Open issues
  • 31 Versions
  • 14 % Grown

The README.md

Laravel API Server Slack Status

A framework for rapid REST API development., (*1)

Installation

  1. Require the repository in your composer.json
  2. Add the ApiServerServiceProvider to your application and publish its config artisan vendor:publish --provider="Fuzz\ApiServer\Providers\ApiServerServiceProvider".
  3. Extend the packaged route provider for your app:
    <?php

    namespace MyApp\Providers;

    use Fuzz\ApiServer\Providers\RouteServiceProvider as ServiceProvider;

    class RouteServiceProvider extends ServiceProvider
    {
        // ...
    }
  1. Extend the packaged exception handler for your app:
    <?php

    namespace MyApp\Exceptions;

    use Fuzz\ApiServer\Exception\Handler as ExceptionHandler;

    class Handler extends ExceptionHandler
    {
        // ...
    }

Usage

Basic usage

Register a base controller extending Fuzz\ApiServer\Routing\Controller:, (*2)

    <?php

    class MyBaseController extends Fuzz\ApiServer\Routing\Controller {}

Register routes pointing to extensions of your base controller. Make a catch-all route to send all other requests through your base controller., (*3)

    <?php

    class MySpecificController extends MyBaseController
    {
        public function someEndpoint() {
            return $this->succeed('Foobar!');
        }
    }

    Route::get('some-endpoint', 'MySpecificController@someEndpoint');
    // ...
    Route::controller(null, 'MyBaseController');

ResourceControllers

Resource controllers extend functionality of fuzz/magic-box repositories and provide CRUD and authorization functionality out of the box., (*4)

Your application should extend the base fuzz/api-server Resource controller:, (*5)

<?php

namespace MyApp\Http\Controllers;

use Fuzz\ApiServer\Routing\ResourceController as BaseResourceController;

class ResourceController extends BaseResourceController
{
    // ...
}

And to define a route for a resource in your routes.php: $router->restful('User');. The restful route macro is defined in Fuzz\ApiServer\Providers\RouteServiceProvider., (*6)

If any resources need to override the default functionality, you can create a specific ResourceController by extending your application's base ResourceController:, (*7)

app/Http/Controllers/Resources/Users.php:, (*8)

<?php

namespace MyApp\Http\Controllers\Resources;

use Illuminate\Http\Request;
use Fuzz\MagicBox\Contracts\Repository;
use MyApp\Http\Controllers\ResourceController;

class Users extends ResourceController
{
    public function index(Repository $repository, Request $request)
    {
        // custom index...
    {
}

You can then point your restful route to your custom ResourceController: in routes.php: $router->restful('Run', 'Resources\Users');, (*9)

Returning that sweet, sweet, data

Send mixed data:, (*10)

    <?php

    return $this->succeed(['foo' => 'bar']);

Send any arrayable data:, (*11)

    <?php

    return $this->succeed(Model::all());

Send any paginated data:, (*12)

    <?php

    return $this->succeed(Model::paginate($this->getPerPage(Model::DEFAULT_PER_PAGE)));

Send RESTful errors with error codes and optional data:, (*13)

    <?php

    $this->badRequest('That button does not do what you think it does.');
    $this->forbidden('Maybe next time.');
    $this->notFound();

Raise RESTful error exceptions outside of the controller context:, (*14)

    <?php

    throw new Symfony\Component\HttpKernel\Exception\ConflictHttpException;
    throw new Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
    throw new Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
    throw new Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException;

Require the user to provide certain parameters:, (*15)

    <?php

    // Magically works with either JSON or form data
    list($foo, $bar) = $this->requireParameters('foo', 'bar');

Read a list of certain parameters:, (*16)

    <?php

    list($foo, $bar) = $this->suggestParameters('foo', 'bar');

Special handling (with de-duplication) for reading arrays:, (*17)

    <?php

    $stuff = $this->requireArrayParameter('stuff');

Handles nested JSON and form properties just fine:, (*18)

    <?php

    // Corresponds with {"foo": {"bar": {"id": 9}}}
    list($foo, $bar_id) = $this->requireParameters('foo', 'foo.bar.id');

CORS Middleware

Configuring the CORS middleware is as simple as adding Fuzz\ApiServer\Routing\CorsMiddleware to the $middleware array in app/Http/Kernel.php., (*19)

The Versions

07/07 2016

dev-open_source_prep

dev-open_source_prep

A framework for rapid REST API development.

  Sources   Download

MIT

The Requires

 

by Fuzz Productions