VOOZH about

URL: https://deepwiki.com/hypervel/devtool/7.1-controller-generation

⇱ Controller Generation | hypervel/devtool | DeepWiki


Loading...
Menu

Controller Generation

This document explains the make:controller command, which generates HTTP controller classes for handling application requests. It covers the command's options, stub template selection logic, model integration features, and automatic FormRequest generation.

For HTTP request validation classes, see Request and Resource Generation. For middleware generation, see Middleware Generation.


Command Overview

The ControllerCommand class provides the make:controller command for generating controller classes in the App\Http\Controllers namespace by default.

Basic Usage:


The command extends GeneratorCommand from Hyperf and implements stub-based code generation with support for multiple controller types: plain, resource, API, and model-bound variants.

Sources: src/Generator/ControllerCommand.php12-17


Controller Stub Types

The command supports five distinct controller stub templates, each designed for specific use cases:

Stub FileDescriptionMethods GeneratedUse Case
controller.plain.stubEmpty controllerNoneCustom implementation
controller.stubFull resource controllerindex, create, store, show, edit, update, destroyTraditional web applications
controller.api.stubAPI resource controllerindex, store, show, update, destroyRESTful APIs (excludes create/edit forms)
controller.model.stubModel-bound resourceindex, create, store, show, edit, update, destroyCRUD with model type hinting
controller.model.api.stubModel-bound APIindex, store, show, update, destroyAPI CRUD with model type hinting

Sources: src/Generator/stubs/controller.plain.stub1-13 src/Generator/stubs/controller.stub1-67 src/Generator/stubs/controller.api.stub1-51 src/Generator/stubs/controller.model.stub1-68 src/Generator/stubs/controller.model.api.stub1-52


Stub Selection Logic

The getStub() method determines which stub template to use based on command options:


Priority Order:

  1. Explicit --type option (highest priority)
  2. Model-bound controller (--model)
  3. Resource controller (--resource)
  4. API controller (--api)
  5. Plain controller (default)

The --api modifier transforms any selected stub by appending .api to its filename, removing create() and edit() methods.

Sources: src/Generator/ControllerCommand.php26-46


Command Options

The make:controller command accepts the following options:

OptionShortcutTypeDescription
--namespace-NOptional valueCustom namespace for the controller class
--api-FlagExclude create and edit methods (API-only)
--type-Required valueManually specify stub file to use
--force-FlagOverwrite existing controller class
--model-mOptional valueGenerate resource controller for given model
--resource-rFlagGenerate resource controller class
--requests-RFlagGenerate FormRequest classes for store/update

Usage Examples:


Sources: src/Generator/ControllerCommand.php53-64


Model Integration

When the --model option is provided, the controller generator integrates model type hinting and automatic route model binding.

Model Resolution Process


The qualifyModel() method resolves the model's fully qualified class name:

  • Strips leading slashes and converts / to \
  • Checks if model already has the configured namespace prefix
  • Prepends App\Models (or configured namespace) if needed

Sources: src/Generator/ControllerCommand.php122-134

Placeholder Replacement

The replaceClass() method performs template variable substitution when a model is specified:

PlaceholderReplacementExample
%NAMESPACED_MODEL%Full model namespaceApp\Models\User
%MODEL%Model class nameUser
%MODEL_VARIABLE%Camelcase variable name$user
%NAMESPACED_REQUESTS%Request class use statementsuse App\Http\Requests\StoreUserRequest;
%STORE_REQUEST%Store request class nameStoreUserRequest
%UPDATE_REQUEST%Update request class nameUpdateUserRequest

Example Generated Method:


Sources: src/Generator/ControllerCommand.php66-95 src/Generator/stubs/controller.model.stub39-42 src/Generator/stubs/controller.model.stub55-57


FormRequest Generation Integration

When both --model and --requests flags are provided, the controller generator orchestrates FormRequest class creation.

Request Generation Flow


The generateFormRequests() method creates two FormRequest classes:

  • Store{Model}Request for create operations
  • Update{Model}Request for update operations

Both classes are generated in the App\Http\Requests namespace.

Generated Controller Methods:


Sources: src/Generator/ControllerCommand.php80-84 src/Generator/ControllerCommand.php97-112


Controller Method Structure

Each controller stub type generates specific method signatures:

Resource Controller (controller.stub)

MethodParametersPurpose
index()NoneDisplay listing of resources
create()NoneShow form for creating new resource
store()Request $requestStore newly created resource
show()string $idDisplay specified resource
edit()string $idShow form for editing resource
update()Request $request, string $idUpdate resource in storage
destroy()string $idRemove resource from storage

Sources: src/Generator/stubs/controller.stub14-65

API Resource Controller (controller.api.stub)

MethodParametersPurpose
index()NoneDisplay listing of resources
store()Request $requestStore newly created resource
show()string $idDisplay specified resource
update()Request $request, string $idUpdate resource in storage
destroy()string $idRemove resource from storage

The API variant excludes create() and edit() methods since APIs typically don't serve HTML forms.

Sources: src/Generator/stubs/controller.api.stub14-49

Model-Bound Controller (controller.model.stub)

Uses model type hints and route model binding:


The $id parameter is replaced with a typed model parameter (e.g., User $user), enabling automatic model resolution through route model binding.

Sources: src/Generator/stubs/controller.model.stub39-65


Configuration

The controller generator respects configuration overrides for namespaces and stub paths:


Configuration Keys

KeyPurposeDefault
namespaceController output namespaceApp\Http\Controllers
model_namespaceModel namespace for resolutionApp\Models
stubCustom stub file pathPackage stubs directory

Configuration is retrieved via getConfig() method inherited from GeneratorCommand.

Sources: src/Generator/ControllerCommand.php48-50 src/Generator/ControllerCommand.php127


Command Orchestration

The controller generator implements the command orchestration pattern through the call() method:


The call() method wraps Application::doRun() to programmatically invoke other commands:

  • Creates ArrayInput with command name and parameters
  • Executes command in the same console application instance
  • Shares output stream for consistent formatting
  • Returns exit status code

This enables the controller generator to automatically create FormRequest classes when the --requests flag is present.

Sources: src/Generator/ControllerCommand.php114-120


Summary

The ControllerCommand provides flexible controller generation through:

  1. Multiple stub types: Plain, resource, API, model-bound variants
  2. Intelligent stub selection: Priority-based logic with --api modifier
  3. Model integration: Type hinting and automatic route model binding
  4. Request generation: Orchestration of FormRequest class creation
  5. Configuration support: Customizable namespaces and stub paths

The command generates controllers in App\Http\Controllers by default, with 5-7 RESTful methods depending on the controller type and options specified.

Sources: src/Generator/ControllerCommand.php1-136