iqbalatma/laravel-utils

此包的最新版本(1.5.3)没有可用的许可证信息。

1.5.3 2024-04-24 10:56 UTC

This package is auto-updated.

Last update: 2024-09-24 11:51:06 UTC


README

Laravel utils 是一组用于提高开发效率的类、辅助函数和命令行工具。这里包含了用于生成枚举、抽象类、接口或特质等的命令行工具

配置

目标目录是生成的文件根路径。您可以按需更改此目标目录位置。

return [
    "target_enum_dir" => "app/Enums",
    "target_trait_dir" => "app/Traits",
    "target_abstract_dir" => "app/Contracts/Abstracts",
    "target_interface_dir" => "app/Contracts/Interfaces",
    "api_response" => [
        "payload_wrapper" => "payload",
        "meta_wrapper" => null
    ],
    "is_show_debug" => env("APP_DEBUG", false)
];

生成文件命令

您可以使用此工具生成枚举,也可以创建带字符串或整数类型的备份枚举

枚举

php artisan make:enum Gender
php artisan make:enum Gender --type=string

您可以使用此命令生成特质

特质

php artisan make:trait HasInstitution

您可以使用此命令生成抽象类

抽象类

php artisan make:abstract BaseService

您可以使用此命令生成接口

接口

php artisan make:inteface IRouter

发布存根文件

在某些情况下,您可能需要发布存根文件并修改模板。

php artisan utils:publish-stub

格式化 API 响应

您可以使用此功能格式化您的 API 响应。例如

<?php

namespace App\Http\Controllers\API\Internal\Management;

use App\Http\APIResponse;
use App\Http\Controllers\ApiController;
use App\Models\Permission;
use App\Http\Resources\Internal\Management\Permissions\PermissionResourceCollection;

class PermissionController extends ApiController
{
    /**
     * @return APIResponse
     */
    public function index(): APIResponse
    {
        $data = Permission::all();

        return $this->response(
            new PermissionResourceCollection($data),
            "Get all data permission successfully"
        );
    }
}

描述

  • 第一个参数是数据,您可以传递分页器、字符串、数组、资源或 null。
  • 第二个参数是消息,数据类型为字符串
  • 第三个参数是响应码。您可以在 Iqbalatma/LaravelUtils/ResponseCode 中查看响应码
  • 第四个参数是错误,通常用于验证错误。
  • 第五个参数是异常,通常用于映射异常。

响应将是

{
    "rc": "SUCCESS",
    "message": "Get all data permission successfully",
    "timestamp": "2023-10-26T23:49:47.387526Z",
    "payload": {
        "data": [
          {
            "id": "99ffccb6-b375-4cf5-9f4c-b6824fabeab3",
            "name": "can show all user",
            "guard_name": "api"
          },
          {
            "id": "99ffccb6-b375-4cf5-9f4c-b6824fabeab4",
            "name": "can show all products",
            "guard_name": "api"
          }
      ]
    }
}

您可以使用第三个参数自定义响应代码(rc)。您可以重写此类以添加此类的响应代码。只需重写类并重写 mapHttpCode() 函数即可。例如,您可以为 ERR_NOT_FOUND() 添加响应代码

<?php

namespace App\Services\V1;

use Iqbalatma\LaravelUtils\Interfaces\ResponseCodeInterface;
use Symfony\Component\HttpFoundation\Response;

/**
 * @method static ResponseCodeInterface ERR_NOT_FOUND()
 */
class ResponseCode extends \Iqbalatma\LaravelUtils\ResponseCode
{
    protected const ERR_NOT_FOUND = "ERR_NOT_FOUND";


    /**
     * @return void
     */
    protected function mapHttpCode(): void
    {
        $this->httpCode = match ($this->name) {
            self::ERR_NOT_FOUND => Response::HTTP_NOT_FOUND,
            default => null
        };

        if ($this->httpCode === null) {
            parent::mapHttpCode();
        }
    }
}

这就是处理异常和自定义响应格式的示例

$this->renderable(function (NotFoundHttpException $e) {
    if (request()->expectsJson()) {
        return new APIResponse(
            null,
            message: $e->getMessage(),
            responseCode: ResponseCode::ERR_NOT_FOUND(),
            exception: $e
        );
    }
});

您可以通过配置文件更改 JSON 格式的行为。

...
"api_response" => [
    "payload_wrapper" => "payload",
    "meta_wrapper" => "meta"
],
"is_show_debug" => env("APP_DEBUG", false)
...
  • 键 is_show_debug 用于确定是否显示异常消息。这可能导致敏感信息泄露,因此请勿在生产环境中启用此功能。
  • 键 api_response.payload 用于响应的键。您可以使用其他包装器或将其设置为 null 以不使用包装器。
  • 键 api_response.meta_wrapper 用于包装数据元。当您使用

辅助函数

ddapi()

您可以使用 ddapi() 辅助函数将 API 数据转储以查看变量的数据

ddapi($data);
ddapi($data, $data2);