devianl2 / tenant-support

1.0.13 2022-06-01 08:50 UTC

This package is auto-updated.

Last update: 2024-08-29 06:07:43 UTC


README

租户支持是一个laravel包,简化并专注于业务功能开发。

对于每个微服务开发,您需要遵循以下说明以确保应用程序标准化。

步骤 1

通过composer安装

composer require devianl2/tenant-support

运行以下命令以导出错误视图模板

php artisan vendor:publish --provider="Tenant\Auth\TenantSupportProvider"

转到 App/Provider/RouteServiceProvider.php 并将速率限制修改为600

protected function configureRateLimiting()
    {
        RateLimiter::for('api', function (Request $request) {
            return Limit::perMinute(600)->by(optional($request->user())->id ?: $request->ip());
        });
    }

步骤 2

转到laravel项目 / app / Exceptions / Handler.php 并修改如下

class Handler extends ExceptionHandler {
protected $dontReport = [
        //
    ];
    ....
}

class Handler extends ExHandler {
// remove all other functions and variables (Exhandler already included all that exceptions)
protected $dontReport = [
        //
    ];
}

步骤 3

所有需要API返回值的控制器,使用 ApiResponse.php 特性。例如:

import Tenant\Support\Traits\ApiResponse
class UserController extends Controller {
    use ApiResponse;
}

## 函数

public function successResponse($data, $statusCode = Response::HTTP_OK)
    {
        return response($data, $statusCode)->header('Content-Type', 'application/json');
    }
    public function errorResponse($errorMessage, $statusCode)
    {
        return response()->json(['error' => $errorMessage, 'error_code' => $statusCode], $statusCode);
    }
    public function errorMessage($errorMessage, $statusCode)
    {
        return response($errorMessage, $statusCode)->header('Content-Type', 'application/json');
    }

步骤 4

所有需要分页的存储库,使用 EloquentPaginate.php 特性。例如:

import Tenant\Support\Traits\EloquentPaginate
class UserModel extends Model {
    use EloquentPaginate;
}

使用 EloquentPaginate 的示例

$templates  =   QuestionTemplate::with([
            'sections'
        ]);

        if (array_key_exists('search', $query) && !empty($query['search']))
        {
            $templates =   $templates->where('title', 'LIKE', '%' . $query['search'] .'%');
        }

        return $this->execute($templates, $paginate, $limit);

## 参数

  1. 查询构建器引用eloquent查询
  2. $paginate 为真/假
  3. $limit 为整数(最小为1)