- How do I install hyperf/resource in a Hyperf application?
- Install the package with Composer using composer require hyperf/resource. After installation, define resource classes and use them from your Hyperf controllers to transform models and collections. Check the package release constraints against your Hyperf framework version before deploying to production.
- Can I use Laravel-style API resource syntax with hyperf/resource?
- Yes, hyperf/resource brings the Laravel-style API resource pattern to Hyperf. You define resource classes with a toArray() contract, conditional attributes, nested resources, and response metadata. It is built for Hyperf conventions rather than Laravel, so you do not need Laravel itself to use the resource pattern.
- How do I create a resource class for a single model in Hyperf?
- Create a class that extends the base resource class provided by hyperf/resource and implement toArray(). Return the array representation of the wrapped model from that method. Your controller can then return the resource instead of exposing the raw model data.
- How do I return a collection of models as a JSON resource?
- Use the package's collection resource support to wrap a collection of models. The collection resource maps each item through its resource class and returns a structured JSON array. This keeps list endpoints consistent with your single-resource output and avoids duplicating transformation logic in controllers.
- How do conditional attributes work with when, merge, and additional?
- Conditional attributes let you include fields only when a given condition is true, typically through the when method. merge adds extra top-level keys to the resource response, while additional provides a way to attach extra attributes or metadata. Together they keep response logic reusable and out of your controllers.
- Does hyperf/resource support nested resources and relationships?
- Yes, the package supports composing nested resources and relationships. You can return another resource instance from a resource's toArray() method to transform related models consistently. Be careful to eager load relationships so nested resources do not introduce N+1 query problems.
- How do I add pagination metadata to a Hyperf API resource response?
- The package includes pagination support for resource collections, allowing pagination metadata to be included with the transformed data. Wrap your paginated result in the appropriate collection resource and return it from the controller. This keeps pagination details consistent with the rest of your JSON response structure.
- How do I test hyperf/resource API responses?
- Because resources expose a toArray() contract, you can instantiate them directly in unit tests and assert the returned array. For endpoint tests, use Hyperf's testing utilities to call routes and assert the JSON structure. Prepare model factories or fixtures so resource inputs are realistic and repeatable.
- Is hyperf/resource suitable for production Hyperf APIs?
- It can be suitable for production Hyperf APIs when you manage query loading and response size carefully. Resources add a small transformation layer, so eager load relationships and avoid exposing sensitive model fields. Review the package's maintenance status and Composer version constraints before relying on it in critical services.
- What are the alternatives to hyperf/resource, and how do I evaluate maintenance?
- Alternatives include writing manual transformer methods in services or controllers, using simple array mappers, or adopting a transformer library such as Fractal if it fits your Hyperf setup. Laravel's API resources are not directly usable in Hyperf because they depend on Laravel components. To evaluate maintenance, check Packagist release history, repository activity, issue handling, and compatibility with your Hyperf version.