diegoalvarezb/laravel-service-utils

用于内部Laravel服务的实用工具。

v1.0.1 2017-09-03 20:07 UTC

This package is auto-updated.

Last update: 2024-09-06 05:04:41 UTC


README

此工具为Laravel(https://laravel.net.cn/)的内部服务添加了一些实用工具。

使用此包,您可以从Diegoalvarezb\ServiceUtils\AbstractService扩展所有您的服务(具有应用业务逻辑的类),并使用一些功能

  • 服务响应接口
  • 日志管理

需求

  • PHP >= 5.6
  • Laravel >= 5.0

安装和配置

使用Composer安装包

composer require diegoalvarezb/laravel-service-utils

并在您的config/app.php文件中添加服务提供者

Diegoalvarezb\FrontMessages\ServiceUtilsServiceProvider::class

此命令将服务-utils配置文件添加到Laravel配置文件夹

php artisan vendor:publish --tag=service-utils

服务响应接口

使用以下命令返回方法的结果

return $this->generateResponse($data = [], $errorCode = 'NO_ERROR', $message = '');

第一个参数包含一个包含所有数据的数组。第二个参数必须是错误代码(此代码必须在服务-utils配置文件中存在)。第三个参数是附加消息(如果您不发送此参数,将选择配置文件中的相应参数)。

此方法将返回一个ServiceResponse对象,该对象具有以下方法

  • hasErrors()
  • isCritical()
  • getMessage()
  • getData()
  • getHttpCode()

服务日志管理

使用以下命令写入日志文件

$this->logException($exception, $type = 'error', $customMessage = '');

第一个参数包含异常。第二个参数必须是日志类型。第三个参数是附加消息。

日志类型列表

  • error
  • emergency
  • alert
  • critical
  • warning
  • notice
  • info

日志结构:[datetime] local.LOG_TYPE: Path\To\Class | method() | (Exception) | message

示例配置文件:service-utils.php

<?php

use Symfony\Component\HttpFoundation\Response;

return [

    /*
     * Error code list for service response interface.
     */
    'service_codes' => [

        'NOT_ERROR' => [
            'is_error' => false,
            'message' => 'Ok.',
            'http_code' => Response::HTTP_OK,
            'is_critial' => false,
        ],

        'GENERAL_ERROR' => [
            'is_error' => true,
            'message' => 'General error.',
            'http_code' => Response::HTTP_INTERNAL_SERVER_ERROR,
            'is_critial' => false,
        ],

        'NEW_ERROR' => [
            'is_error' => true,
            'message' => 'Text example of a new error.',
            'http_code' => Response::HTTP_INTERNAL_SERVER_ERROR,
            'is_critial' => true,
        ],

    ],

];

许可证

MIT