softonic/rest-api-nested-resources

用于处理嵌套资源的REST API的实用工具

1.0.2 2022-05-31 12:16 UTC

This package is auto-updated.

Last update: 2024-08-29 06:28:17 UTC


README

Latest Version Software License Build Status Total Downloads Average time to resolve an issue Percentage of issues still open

用于处理嵌套资源的REST API的实用工具

主要功能

  • MultiKeyModel: 允许使用复合主键的嵌套资源
  • EnsureModelExists: 中间件,用于验证资源是否存在(用于确保父资源存在)
  • EnsureModelDoesNotExist: 中间件,用于验证我们想要创建的资源尚未存在
  • SubstituteBindings: Laravel的SubstituteBindings中间件的个性化定制,以处理嵌套资源
  • SplitPutPatchVerbs: 允许控制器将“更新”方法拆分为“修改”(PATCH)和“替换”(PUT)CRUDL方法的特性

安装

您可以使用composer要求包的最后一个版本

composer require softonic/rest-api-nested-resources

配置

  • MultiKeyModel
class UserCommentModel extends MultiKeyModel
{
    /**
     * Identifiers to be hashed and used in the real primary and foreign keys.
     */
    protected static array $generatedIds = [
        'id_user_comment' => [
            'id_user',
            'id_comment',
        ],
    ];
}
  • EnsureModelExists和EnsureModelDoesNotExist
class UserCommentController extends Controller
{
    protected function setMiddlewares(Request $request)
    {
        $this->middleware(
            'App\Http\Middleware\EnsureModelExists:App\Models\User,id_user',
            ['only' => ['store', 'update']]
        );

        $this->middleware(
            'App\Http\Middleware\EnsureModelDoesNotExist:App\Models\UserComment,id_user,id_comment',
            ['only' => 'store']
        );
    }
}
  • SubstituteBindings
use App\Models\UserComment;

class UserCommentController extends Controller
{
    public function show(UserComment $userComment)
    {
        ...
    }
}
  • SplitPutPatchVerbs
use App\Models\UserComment;

class UserCommentController extends Controller
{
    use SplitPutPatchVerbs;

    public function modify(UserComment $userComment, Request $request)
    {
        ...
    }

    public function replace(Request $request, string $id_user, string $id_comment)
    {
        ...
    }
}

测试

softonic/rest-api-nested-resources有一个PHPUnit测试套件,以及使用PHP CS Fixer的编码风格合规性测试套件。

要从项目目录中运行测试,请执行以下命令。

$ make tests

在开发环境中打开终端

$ make debug

许可证

Apache 2.0许可证。有关更多信息,请参阅LICENSE