aqjw/resource-typeable
Composer 安装命令:
composer require aqjw/resource-typeable
包简介
Extends the capabilities of the Laravel resource to separate it into types.
README 文档
README
This package extends the Laravel resource to separate it into types.
Installation
Require this package with composer:
composer require aqjw/resource-typeable
Usage
To turn your resource into typeable, just use ResourceTypeable trait.
use Aqjw\ResourceTypeable\ResourceTypeable; ... class ProductResource extends JsonResource { use ResourceTypeable; ...
Now your resource has typeable capabilities.
Example
use Aqjw\ResourceTypeable\ResourceTypeable; ... class ProductResource extends JsonResource { use ResourceTypeable; /** * Tiny product resource * * @return array */ public function tiny($request): array { return [ 'id' => $this->id, 'title' => $this->title, 'slug' => $this->slug, 'price' => $this->price, ]; } /** * Full product resource * * @return array */ public function full($request): array { return array_merge( $this->tiny($request), [ 'brand' => $this->brand, 'model' => $this->model, 'material' => $this->material, 'size' => $this->size, 'tags' => $this->tags, 'color' => $this->color, 'print' => $this->print, ], ); } }
In the controller, you can call the resource as before.
... class ProductController extends Controller { ... public function index() { $products = Product::paginate(9); return new ProductCollection($products); // will return tiny resource type // or return ProductCollection::make($products); // will return tiny resource type // or return ProductResource::collectionType('full', $products); // will return full resource type } ...
One more example:
... class ProductController extends Controller { ... public function show(Product $product) { return ProductResource::makeType('full', $product); } ...
Configuration
By default the resource type is tiny, but you can change it with $resource_type variable declaration:
use Aqjw\ResourceTypeable\ResourceTypeable; ... class ProductResource extends JsonResource { use ResourceTypeable; /** * Resource type * * @var string */ protected static $resource_type = 'full'; ...
Tips
Resource types are unlimited, you can create any names for them, not just tiny and full.
But keep in mind that they should not override the parent's methods.
License
The MIT License (MIT). Please see License File for more information.
aqjw/resource-typeable 适用场景与选型建议
aqjw/resource-typeable 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 13 次下载、GitHub Stars 达 0, 最近一次更新时间为 2022 年 06 月 26 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「laravel」 「resource」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 aqjw/resource-typeable 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 aqjw/resource-typeable 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 aqjw/resource-typeable 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Generates URLs for public Puli resources.
Add the resource system to symfony application
A lightweight and powerful OAuth 2.0 authorization and resource server library with support for all the core specification grants. This library will allow you to secure your API with OAuth and allow your applications users to approve apps that want to access their data from your API.
Adds support for singular resource routes to the Laravel router.
Retrieve a WebResourceInterface instance over HTTP
Common Resource Api Interface
统计信息
- 总下载量: 13
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 10
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2022-06-26