lushdigital/microservice-crud
Composer 安装命令:
composer require lushdigital/microservice-crud
包简介
A CRUD convenience layer for microservices built in Lumen.
README 文档
README
A CRUD convenience layer for microservices built in Lumen.
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.
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:
$ composer require lushdigital/microservice-crud
The package requires that the following changes are made to the Lumen config in bootstrap/app.php
<?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.
<?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
<?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:
<?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:
$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'.
This can be changed by overriding the $modelBaseClass attribute in the controller:
<?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).
This can be changed by overriding the $modelNamespace attribute:
<?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:
examples:index examples:1
Note the
:indexcache key is always used for the indexing endpoint listing all model instances.
To cache based on other attributes just override the $attributeCacheKeys attribute in your model:
<?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
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:
<?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');
lushdigital/microservice-crud 适用场景与选型建议
lushdigital/microservice-crud 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 3.93k 次下载、GitHub Stars 达 7, 最近一次更新时间为 2017 年 02 月 14 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「framework」 「crud」 「laravel」 「Microservice」 「lumen」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 lushdigital/microservice-crud 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 lushdigital/microservice-crud 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 lushdigital/microservice-crud 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
PHP Framework HLEB2 is the foundation of the web application. Provides ease of development and application performance.
Gii Generator for double model generation
A PSR-7 compatible library for making CRUD API endpoints
Admin panel generator for Laravel 8 and based on Vuetify Admin, a separate SPA admin framework running on top of REST APIs.
Collection of tools to use the full power of the Enyalius framework
Laravel Admin CRUD Generator
统计信息
- 总下载量: 3.93k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 7
- 点击次数: 11
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2017-02-14