2017 © Pedro Peláez
 

library microservice-crud

A CRUD convenience layer for microservices built in Lumen.

image

lushdigital/microservice-crud

A CRUD convenience layer for microservices built in Lumen.

  • Tuesday, March 6, 2018
  • by danmrichards
  • Repository
  • 2 Watchers
  • 5 Stars
  • 3,097 Installations
  • PHP
  • 1 Dependents
  • 0 Suggesters
  • 2 Forks
  • 0 Open issues
  • 11 Versions
  • 5 % Grown

The README.md

Lush Digital - CRUD

A CRUD convenience layer for microservices built in Lumen., (*1)

Description

This package is intended to provide a convenient layer on top of Lumen to streamline the process of developing a RESTful CRUD microservice. It reduces the repetitive nature of writing controllers, models and CRUD logic over and over again., (*2)

Package Contents

  • An abstract controller which can be extended for quick RESTful CRUD logic.
  • Cache clearing observer to ensure your data always appears as up-to-date as possible.

Installation

Install the package as normal:, (*3)

$ composer require lushdigital/microservice-crud

The package requires that the following changes are made to the Lumen config in bootstrap/app.php, (*4)

<?php

// Uncomment the line below to enable Facade support.
$app->withFacades();

// Uncomment the line below to enable Eloquent ORM support.
$app->withEloquent();

// Add the line below to load database config. This is required for caching to work.
$app->configure('database');

Usage

To create a new CRUD resource first extend your model from \LushDigital\MicroServiceModelUtils\Models\MicroServiceBaseModel. The model must also implement the LushDigital\MicroServiceCrud\Models\CrudModelInterface interface., (*5)

<?php 

namespace App\Models;

use LushDigital\MicroServiceModelUtils\Models\MicroServiceBaseModel;
use LushDigital\MicroServiceCrud\Models\CrudModelInterface;

class Example extends MicroServiceBaseModel implements CrudModelInterface
{
    /**
     * {@inheritdoc}
     */
    public function getValidationRules($mode = 'create', $primaryKeyValue = null)
    {
        // TODO: Implement getValidationRules() method.
    }
}

Next you need to create a controller which extends from \LushDigital\MicroServiceCrud\Http\Controllers\CrudController, (*6)

<?php 

namespace App\Http\Controllers;

use LushDigital\MicroServiceCrud\Http\Controllers\CrudController;

class ExamplesController extends CrudController {}

Finally if you are using caching you need to register the Crud observer against your model. Do this by editing the existing app/Providers/AppServiceProvider.php class (or add a new service provider) like so:, (*7)

<?php

namespace App\Providers;

use App\Models\Example;
use Illuminate\Support\ServiceProvider;
use LushDigital\MicroServiceCrud\Observers\CrudObserver;

class AppServiceProvider extends ServiceProvider
{
    /**
     * Bootstrap any application services.
     *
     * @return void
     */
    public function boot()
    {
        // Add an observer to the example model.
        Example::observe(CrudObserver::class);
    }
}

Make sure that your service provider is added (or uncommented) in bootstrap/app.php:, (*8)

$app->register(App\Providers\AppServiceProvider::class);

Model

Note that above the model is called Example and the controller is called ExamplesController. This follows the plural pattern you're used to with Laravel. Basically the controller name needs to be the plural version of the model plus 'Controller'., (*9)

This can be changed by overriding the $modelBaseClass attribute in the controller:, (*10)

<?php 

namespace App\Http\Controllers;

use LushDigital\MicroServiceCrud\Http\Controllers\CrudController;

class ExamplesController extends CrudController 
{
    /**
     * The model associated with the CRUD controller.
     * 
     * @var string  
     */
    protected $modelBaseClass = 'NotAnExample';
}

Model Namespace

The CRUD controller assumes the model exists in the App\Models namespace (e.g. App\Controllers\ExamplesController => App\Models\Example)., (*11)

This can be changed by overriding the $modelNamespace attribute:, (*12)

<?php 

namespace App\Http\Controllers;

use LushDigital\MicroServiceCrud\Http\Controllers\CrudController;

class ExamplesController extends CrudController 
{
    /**
     * The model associated with the CRUD controller.
     * 
     * @var string  
     */
    protected $modelNamespace = 'My\\Awesome\\Namespace';
}

Cache Attributes

The package provides support for caching data based on any attribute of a model. By default the package will cache data using the id attribute only. This works using cache keys, so for an instance of the Example model with id 1 the following cache keys will be set on read and cleared on update/delete:, (*13)

examples:index
examples:1

Note the :index cache key is always used for the indexing endpoint listing all model instances., (*14)

To cache based on other attributes just override the $attributeCacheKeys attribute in your model:, (*15)

<?php 

namespace App\Models;

use LushDigital\MicroServiceModelUtils\Models\MicroServiceBaseModel;

class Example extends MicroServiceBaseModel
{
    /**
     * A list of the model attributes that can be used as cache keys.
     *
     * @var array
     */
    protected $attributeCacheKeys = ['name'];
}

So in this cache Example (ID: 1) with a name of 'test' would have a cache key of examples:name:test, (*16)

Routing

Once your controller and model are set up you need to configure routes. The package provides logic for the standard REST endpoints (GET, POST, PUT, DELETE). You can use as many or as few as you like, an example of all routes would be:, (*17)

<?php

// routes/web.php
$app->get('/examples', 'ExamplesController@index');
$app->post('/examples', 'ExamplesController@store');
$app->get('/examples/{id}', 'ExamplesController@show');
$app->put('/examples/{id}', 'ExamplesController@update');
$app->delete('/examples/{id}', 'ExamplesController@destroy');

The Versions

06/03 2018

dev-master

9999999-dev

A CRUD convenience layer for microservices built in Lumen.

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel microservice framework lumen crud

06/03 2018

v3.0.0

3.0.0.0

A CRUD convenience layer for microservices built in Lumen.

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel microservice framework lumen crud

12/10 2017

v2.0.2

2.0.2.0

A CRUD convenience layer for microservices built in Lumen.

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel microservice framework lumen crud

22/08 2017

v2.0.1

2.0.1.0

A CRUD convenience layer for microservices built in Lumen.

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel microservice framework lumen crud

28/07 2017

v2.0.0

2.0.0.0

A CRUD convenience layer for microservices built in Lumen.

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel microservice framework lumen crud

17/07 2017

v1.0.1

1.0.1.0

A CRUD convenience layer for microservices built in Lumen.

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel microservice framework lumen crud

28/04 2017

v1.0.0

1.0.0.0

A CRUD convenience layer for microservices built in Lumen.

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel microservice framework lumen crud

10/04 2017

v0.1.3

0.1.3.0

A CRUD convenience layer for microservices built in Lumen.

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel microservice framework lumen crud

03/03 2017

v0.1.2

0.1.2.0

A CRUD convenience layer for microservices built in Lumen.

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel microservice framework lumen crud

23/02 2017

v0.1.1

0.1.1.0

A CRUD convenience layer for microservices built in Lumen.

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel microservice framework lumen crud

14/02 2017

v0.1.0

0.1.0.0

A CRUD convenience layer for microservices built in Lumen.

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel microservice framework lumen crud