All Projects → gpasztor87 → oc-api-plugin

gpasztor87 / oc-api-plugin

Licence: MIT license
Tools for building RESTful HTTP + JSON APIs for OctoberCMS.

Programming Languages

PHP
23972 projects - #3 most used programming language

Projects that are alternatives of or similar to oc-api-plugin

contenteditor-plugin
Edit your content in page - plugin for OctoberCMS
Stars: ✭ 32 (+14.29%)
Mutual labels:  octobercms, octobercms-plugin
oc-gdpr-plugin
October CMS plugin to make websites GDPR and ePrivacy compliant
Stars: ✭ 32 (+14.29%)
Mutual labels:  octobercms, octobercms-plugin
octobercms-backendskin
Backend Skin Plugin For OctoberCms
Stars: ✭ 21 (-25%)
Mutual labels:  octobercms, octobercms-plugin
oc-sortablerelations-plugin
Adds drag and drop sorting functionality to the view list of the relations controller in October CMS
Stars: ✭ 16 (-42.86%)
Mutual labels:  octobercms, octobercms-plugin
oc-site-search-plugin
Adds global search capabilities to October CMS.
Stars: ✭ 39 (+39.29%)
Mutual labels:  octobercms, octobercms-plugin
oc-responsive-images-plugin
Adds reponsive images capabilities to October CMS
Stars: ✭ 37 (+32.14%)
Mutual labels:  octobercms, octobercms-plugin
oc-speedy-plugin
Website optimization plugin for October CMS
Stars: ✭ 18 (-35.71%)
Mutual labels:  octobercms, octobercms-plugin
oc-backup-plugin
Backup system for October CMS
Stars: ✭ 18 (-35.71%)
Mutual labels:  octobercms, octobercms-plugin
Docktober
🍂 Simple: Docker + OctoberCMS
Stars: ✭ 57 (+103.57%)
Mutual labels:  octobercms
oc-bootstrapper
Easily bootstrap a new October CMS project
Stars: ✭ 86 (+207.14%)
Mutual labels:  octobercms
oc-mall-theme
Demo theme for the oc-mall-plugin
Stars: ✭ 19 (-32.14%)
Mutual labels:  octobercms
oc-api-plugin
Base API Plugin for OctoberCMS
Stars: ✭ 26 (-7.14%)
Mutual labels:  octobercms
awesome-october-cms
A curated list of awesome things related to OctoberCMS.
Stars: ✭ 19 (-32.14%)
Mutual labels:  octobercms
debugbar-plugin
Integrates PHP Debugbar with October CMS
Stars: ✭ 36 (+28.57%)
Mutual labels:  octobercms
oc-menumanager-plugin
A Menu Management Plugin for October CMS
Stars: ✭ 29 (+3.57%)
Mutual labels:  octobercms
oc-imageresize-plugin
October Plugin - Image Resize
Stars: ✭ 13 (-53.57%)
Mutual labels:  octobercms-plugin
oc-twigextensions-plugin
Twig extensions plugin for OctoberCMS
Stars: ✭ 17 (-39.29%)
Mutual labels:  octobercms-plugin
oc-good-news-plugin
📰 News (blogging) plugin for October CMS
Stars: ✭ 23 (-17.86%)
Mutual labels:  octobercms
October
Self-hosted CMS platform based on the Laravel PHP Framework.
Stars: ✭ 10,740 (+38257.14%)
Mutual labels:  octobercms
sveltober
Cybernetically enhanced October applications
Stars: ✭ 19 (-32.14%)
Mutual labels:  octobercms

#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.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].