rustyphp/microservice-crud

Lumen构建的微服务CRUD便捷层。

v3.0.3 2019-10-15 13:49 UTC

This package is not auto-updated.

Last update: 2024-09-21 21:47:12 UTC


README

Lumen构建的微服务CRUD便捷层。

描述

此包旨在在Lumen之上提供一个方便的层,以简化开发RESTful CRUD微服务的过程。它减少了编写控制器、模型和CRUD逻辑的重复性。

包内容

  • 一个抽象控制器,可以扩展以快速实现RESTful CRUD逻辑。
  • 缓存清理观察者以确保数据始终尽可能显示为最新。

安装

按正常方式安装包

$ composer require lushdigital/microservice-crud

该包要求对bootstrap/app.php中的Lumen配置进行以下更改

<?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');

用法

要创建新的CRUD资源,首先从\LushDigital\MicroServiceModelUtils\Models\MicroServiceBaseModel扩展您的模型。该模型还必须实现LushDigital\MicroServiceCrud\Models\CrudModelInterface接口。

<?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.
    }
}

接下来,您需要创建一个扩展自\LushDigital\MicroServiceCrud\Http\Controllers\CrudController的控制器

<?php 

namespace App\Http\Controllers;

use LushDigital\MicroServiceCrud\Http\Controllers\CrudController;

class ExamplesController extends CrudController {}

最后,如果您使用缓存,则需要将Crud观察者注册到您的模型上。通过编辑现有的app/Providers/AppServiceProvider.php类(或添加新的服务提供者)来完成此操作

<?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);
    }
}

确保您的服务提供者已添加(或取消注释)在bootstrap/app.php

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

模型

注意,上面的模型被称为Example,控制器被称为ExamplesController。这遵循您习惯的Laravel的复数模式。基本上,控制器名称需要是模型的复数形式加上'Controller'。

这可以通过在控制器中覆盖$modelBaseClass属性来更改

<?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';
}

模型命名空间

CRUD控制器假定模型位于App\Models命名空间中(例如App\Controllers\ExamplesController => App\Models\Example)。

这可以通过覆盖$modelNamespace属性来更改

<?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';
}

缓存属性

该包提供基于模型任何属性的缓存支持。默认情况下,该包将仅使用id属性缓存数据。这是通过缓存键实现的,因此对于id为1的Example模型实例,以下缓存键将在读取时设置,在更新/删除时清除

examples:index
examples:1

注意,:index缓存键始终用于索引端点列出所有模型实例。

要基于其他属性进行缓存,只需在您的模型中覆盖$attributeCacheKeys属性

<?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'];
}

因此,具有名称为'test'的此缓存示例(ID:1)将具有缓存键examples:name:test

路由

一旦设置好控制器和模型,您需要配置路由。该包提供了标准REST端点(GET、POST、PUT、DELETE)的逻辑。您可以使用任意多或任意少,所有路由的示例将是

<?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');