VOOZH about

URL: https://deepwiki.com/hypervel/devtool/7.3-request-and-resource-generation

⇱ Request and Resource Generation | hypervel/devtool | DeepWiki


Loading...
Menu

Request and Resource Generation

This document covers the make:request and make:resource generator commands, which produce HTTP layer components for handling incoming request validation and outgoing API response transformation. The RequestCommand generates form request classes for validation and authorization logic, while the ResourceCommand generates API resource transformers for converting models into JSON responses.

For controller generation that may use these requests and resources, see Controller Generation. For the broader HTTP layer overview, see HTTP Layer Generators.


System Overview

The request and resource generation system consists of two independent generator commands that produce distinct types of HTTP components:

CommandPurposeOutput LocationBase Class
make:requestForm request validationApp\Http\RequestsHypervel\Foundation\Http\FormRequest
make:resourceAPI response transformationApp\Http\ResourcesHypervel\Http\Resources\Json\JsonResource or ResourceCollection

Both commands extend Hyperf\Devtool\Generator\GeneratorCommand and follow the standard stub-based generation pattern.


Sources: src/Generator/RequestCommand.php1-32 src/Generator/ResourceCommand.php1-47


Form Request Generation

RequestCommand Implementation

The RequestCommand class provides the make:request command for generating form request validation classes. This command creates classes that encapsulate authorization logic and validation rules for incoming HTTP requests.

Key Implementation Details:

MethodReturn ValueDescription
__construct()voidRegisters command as make:request
getStub()stringReturns path to request.stub template
getDefaultNamespace()stringReturns App\Http\Requests

The command supports standard generator options inherited from GeneratorCommand, including namespace customization and force overwrite flags.

Sources: src/Generator/RequestCommand.php9-32

Request Stub Template

The form request stub defines the structure for generated request classes:


The generated class includes two primary methods:

  1. authorize(): bool - Authorization logic that determines if the user can make the request. Default implementation returns true, allowing all requests.

  2. rules(): array - Validation rules array that defines constraints for request data. Default implementation returns an empty array with a comment placeholder.

Template Structure:

  • Declares strict types
  • Imports Hypervel\Foundation\Http\FormRequest
  • Provides skeleton methods for authorization and validation
  • Uses PSR-12 coding standards

Sources: src/Generator/stubs/request.stub1-28

Generated Request Class Example

When executing php bin/hyperf.php make:request StoreUserRequest, the generator produces:

namespace: App\Http\Requests
class name: StoreUserRequest
file path: app/Http/Requests/StoreUserRequest.php

The class extends Hypervel\Foundation\Http\FormRequest and provides two customization points where developers add authorization logic and validation rules.

Sources: src/Generator/RequestCommand.php28-31 src/Generator/stubs/request.stub1-28


API Resource Generation

ResourceCommand Implementation

The ResourceCommand class provides the make:resource command for generating API resource transformer classes. These classes convert models and collections into JSON API responses with controlled data exposure.

Command Architecture:


Stub Selection Logic src/Generator/ResourceCommand.php24-32:

The command determines which stub to use based on:

  1. Whether the class name ends with the string Collection
  2. Whether the --collection option is provided

If either condition is true, the resource-collection.stub template is used; otherwise, the standard resource.stub template is used.

Sources: src/Generator/ResourceCommand.php10-47

Resource Stub Templates

Single Resource Template

The single resource stub generates classes for transforming individual model instances:

Template Characteristics:

  • Extends Hypervel\Http\Resources\Json\JsonResource
  • Provides toArray(): array method for data transformation
  • Default implementation calls parent::toArray() for basic attribute exposure

Sources: src/Generator/stubs/resource.stub1-19

Resource Collection Template

The collection stub generates classes for transforming collections of models:

Template Characteristics:

  • Extends Hypervel\Http\Resources\Json\ResourceCollection
  • Provides toArray(): array method for collection transformation
  • Default implementation calls parent::toArray() for basic collection handling
  • Automatically wraps individual items in the corresponding resource class

Sources: src/Generator/stubs/resource-collection.stub1-19

Resource Command Options

The ResourceCommand supports the following options:

OptionShortTypeDescription
--namespace-NVALUE_OPTIONALCustom namespace for the resource class
--force-fVALUE_NONEOverwrite existing resource class
--collection-cVALUE_NONEGenerate a resource collection class

Usage Examples:


Sources: src/Generator/ResourceCommand.php39-46


Configuration and Customization

Both commands support configuration overrides through the generator configuration system:

Request Configuration


Sources: src/Generator/RequestCommand.php23-30

Resource Configuration


Sources: src/Generator/ResourceCommand.php24-36


Class Hierarchy and Dependencies


Sources: src/Generator/RequestCommand.php1-32 src/Generator/ResourceCommand.php1-47 src/Generator/stubs/request.stub7 src/Generator/stubs/resource.stub7 src/Generator/stubs/resource-collection.stub7


Generated Class Structure Comparison

AspectForm RequestSingle ResourceResource Collection
Base ClassFormRequestJsonResourceResourceCollection
Primary Methodrules()toArray()toArray()
Secondary Methodauthorize()--
Use CaseInput validationModel → JSONCollection → JSON
Typical LocationController method paramsController returnController return for lists
Default NamespaceApp\Http\RequestsApp\Http\ResourcesApp\Http\Resources

Sources: src/Generator/stubs/request.stub1-28 src/Generator/stubs/resource.stub1-19 src/Generator/stubs/resource-collection.stub1-19


Integration with Model Generation

The ModelCommand can automatically generate request classes when invoked with specific flags. When using make:model User --requests or make:model User --all, the system generates StoreUserRequest and UpdateUserRequest classes alongside the model. This integration is handled by the ModelCommand orchestrator.

For details on this integration, see Model and Factory Generation.

Sources: src/Generator/RequestCommand.php1-32 src/Generator/ResourceCommand.php1-47