#66

Product support

Visit this product's website for support.

Categories

  • Developer Tools

This plugin allows you to create RESTful controllers.

In a nutshell, it adds the following scaffold command.

php artisan create:restcontroller Acme.Plugin ControllerName

However, it does more than just that, you have the ability to override the api actions (verbs) with your own logic. The current version of the plugin supports default behavior logic for several common verbs — create, store, show, edit, update, destroy. Most RESTful APIs use custom logic and hide several fields so you can override the behavior than using the default behavior. Simply create a function in your generated RESTful controller with the same name as the verb i.e. show, update, etc. and it will get overridden.

Feel free to make PRs to the github repository should you want to contribute. If you have feature requests use the issue tracker.

Plugins using RESTful
Author Name Plugin
Saifur Rahman Mohsin Mobile plugin
Saifur Rahman Mohsin Mobile User plugin
Saifur Rahman Mohsin Badge plugin

If you would like to list your plugin in this section, drop me an email with your plugin link and I’ll add it here. It’s free SEO :)

Adding RESTful behavior to existing controller (Without scaffolding)

If you prefer not to use the http folder and wanted all the REST API logic mixed in the same controller generated by the create:controller command you may do so. In order to use the REST behavior you should add it to the $implement property of the controller class. Also, the $restConfig class property should be defined and its value should refer to the YAML file used for configuring the behavior options.

namespace Acme\Blog\Controllers;

class Categories extends \Backend\Classes\Controller
{
    public $implement = ['Mohsin.Rest.Behaviors.RestController'];

    public $restConfig = 'rest_config.yaml';
}

Next you must configure the RESTful behavior. The configuration file referred in the $restConfig property is defined in YAML format. The file should be placed into the controller's views directory. Below is an example of a typical RESTful behavior configuration file:

# ===================================
#  Rest Behavior Config
# ===================================

# Allowed Rest Actions
allowedActions:
  index:
      pageSize: 20
  store:
  show:
  update:
  destroy:

# Model Class name
modelClass: Acme\Blog\Models\Post

# Verb Method Prefix
prefix: api

The prefix ensures that the controller actions do not collide with the existing actions of the controller. Example actions are index, update, preview which several behaviors such as the ListController and FormController behaviors use. The RESTful functions should not interfere with these methods so you can ensure this by using the prefix. So, in order to override the default logic you would write the method signature as the prefix followed by camel case verb name. For example, public function apiStore().

Finally create a routes.php file. Here’s the general template:

<?php

Route::group(['prefix' => 'api/v1'], function () {
    Route::resource(‘posts', ‘Acme\Blog\Controller\Posts');
});

And it should work fine. I however recommend you to skip all these steps and instead use the scaffolding command, it’s much simpler.

  • Found the plugin useful on 15 Aug, 2017

    this plugin is cool, I love it. just one thing, I cannot find a way to sort data by alphabetical order when retrieving data. any thoughts?

  • Found the plugin useful on 5 Jun, 2017

    Can you make this plugin not response a 'Set-Cookie' in the header for some routes? since I want keep this stateless, and sometime I am developing with iOS or Android, or 微信小程序 ... quite annoying

  • Found the plugin useful on 31 Jan, 2017

    5 stars plugin first. This is very simple and powerful plugin. Highly recommended for implementing RESTful API. Waiting for the next version. Thanks!

  • Found the plugin useful on 24 Oct, 2016

    Simple but very effective! Thanks for this plugin :)

  • Found the plugin useful on 23 Sep, 2016

    Thank for your plugin! <3

  • Found the plugin useful on 26 Aug, 2016

    Wonderful! I think this may evolve into something really brilliant!

    Thanks!

1.0.3

Added support for querying relations for the index and show verbs.

Oct 03, 2016

1.0.2

!!! Contains breaking changes and adds default behaviour for index, store, show, update, destroy verbs.

Sep 27, 2016

1.0.1

First version of RESTful

Aug 17, 2016