Git Product home page Git Product logo

oc-api-plugin's Introduction

#Api plugin

Build Status Scrutinizer Code Quality StyleCI

Tools for building RESTful HTTP + JSON APIs for OctoberCMS.

Introduction

This plugin provides two features

  1. Console generator which creates Controller and Fractal Transformer in a single command.

  2. Basic REST API skeleton that can be really helpful if you need something standard. It's 100% optional.

If you do not use Fractal for your transformation layer, this plugin is probably not the right choice for you.

Installation

  • Extract this repository into plugins/autumn/api
  • In plugins/autumn/api folder run composer install.

Usage

Generator

The only console command that is added is artisan create:api <AuthorName>.<PluginName> <ModelName>.

Imagine you need to create a rest api to list/create/update etc posts from Acme.Blog plugin. To achieve that you need to do lots of boilerplate operations - create controller, transformer, set up needed routes.

php artisan create:api Acme.Blog Post does all the work for you.

  1. The generator creates a routes.php which looks like that:
<?php

    Route::group(['prefix' => 'api/v1'], function() {
        //
        Route::resource('posts', 'Acme\Blog\Controllers\Posts');
    });
    
  1. The generator creates a controller that extends base api controller. In the controller you can register your API endpoints via $publicActions property:
<?php

namespace Acme\Blog\Http\Controllers;

use Acme\Blog\Models\Post;
use Acme\Blog\Http\Transformers\PostTransformer;
use Autumn\Api\Classes\ApiController;

class PostsController extends ApiController
{   
    /**
     * Eloquent model.
     *
     * @return \October\Rain\Database\Model
     */
    protected function model()
    {
        return new Post;
    }
    
    /**
     * Transformer for the current model.
     *
     * @return \League\Fractal\TransformerAbstract
     */
    protected function transformer()
    {
        return new PostTransformer;
    }
}

You can customize this stub as much as you want.

  1. Finally the generator creates a fractal Transformer
<?php

namespace Acme\Blog\Http\Transformers;

use Acme\Blog\Models\Post;
use League\Fractal\TransformerAbstract;

class PostTransformer extends TransformerAbstract
{   
    /**
     * Turn this item object into a generic array.
     *
     * @param $item
     * @return array
     */
    public function transform(Post $item)
    {
        return [
            'id'         => (int)$item->id,
            'created_at' => (string)$item->created_at,
            'updated_at' => (string)$item->updated_at,
        ];
    }
}

This stub is customizable too.

The list of routes that are available out of the box:

  1. GET api/v1/blog/posts
  2. GET api/v1/blog/posts/{id}
  3. POST api/v1/blog/posts
  4. PUT api/v1/blog/posts/{id}
  5. DELETE api/v1/blog/posts/{id}

Request and response format is json Fractal includes are supported via $_GET['include']. Validation rules for create and update can be set by overwriting rulesForCreate and rulesForUpdate in your controller.

This skeleton is not a silver bullet but in many cases it can be either exactly what you need or can be used as a decent starting point for your api.

You can check https://github.com/gpasztor87/oc-api-plugin/blob/master/classes/ApiController.php for more info.

Rate Limiting

Rate Limiting (throttling) allows you to limit the number of requests a client can make in a given amount of time. A limit and the expiration time is defined by a throttle. By default the package has two throttles, an authenticated throttle and an unauthenticated throttle.

Enabling Rate Limiting

To enable rate limiting for a route or group of routes you must enable the api.throttle middleware.

    Route::group(['prefix' => 'api/v1', 'middleware' => ['api.throttle:100,5']]], function() {
        // Routes
    });

This would set a request limit of 100 with an expiration time of 5 minutes for this group.

oc-api-plugin's People

Contributors

gpasztor87 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

oc-api-plugin's Issues

JWT Auth

Is it possible to use your JWT Auth plugin in this cool API plugin?

Update method

When I use the update method, it returns 404. I have added the method to the $publicActions. The URL the request is hitting is: website.com/api/v1/products/2 with method PUT. What am I doing wrong?

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.