Skip to content

ide.json: Code Generations

Overview

The codeGenerations section lets you define custom code generation actions that appear in the Laravel Idea Code generations menu. Each entry describes a generation wizard: what the user inputs, what files get created, and which template is used to fill them.

json
{
    "$schema": "https://laravel-ide.com/schema/laravel-ide-v2.json",
    "codeGenerations": [
        {
            "id": "my-package:handler",
            "name": "Create PackageName Handler",
            "files": [ ... ]
        }
    ]
}

Top-level fields

FieldRequiredDescription
idYesUnique identifier for this generation.
nameYesDisplay name.
classSuffixNoSuffix automatically appended to the generated class name (e.g. "Post"PostController).
inputFilterNoValidation mode for the user's input. See Input filters. Default: "classNameWithNamespace".
regexNoCustom regex that the input must match (overrides inputFilter).
parametersNoExtra UI fields shown in the wizard. See Parameters.
filesNoFiles to generate. See Files.

Input filters

ValueDescription
classNameWithNamespaceAccepts a PHP class name, optionally with a namespace (e.g. Http/Requests/StoreUser). Default.
classNameAccepts a simple class name without a namespace.
migrationAccepts a Laravel migration name (e.g. create_users_table).

Parameters

Each entry in parameters adds an extra input field to the generation dialog.

FieldRequiredDescription
idYesUnique identifier for this parameter.
nameYesLabel shown in the UI.
typeYesUI control type. See Parameter types.
variableYesTemplate variable name (used as ${VARIABLE} in templates).
optionalNoWhether the field can be left empty. Default: true.
defaultValueNoPre-filled value.
optionsNoKey→label map for comboBox type.

Parameter types

TypeDescription
stringText input field.
checkboxBoolean checkbox.
eloquentClassClass picker limited to Eloquent models.
projectClassClass picker for any PHP class in the project.
comboBoxDropdown. Requires at least 2 entries in options.
json
{
    "$schema": "https://laravel-ide.com/schema/laravel-ide-v2.json",
    "codeGenerations": [
        {
            "id": "my-handler",
            "name": "My Handler",
            "parameters": [
                {
                    "id": "model",
                    "name": "Model class",
                    "type": "eloquentClass",
                    "variable": "MODEL_CLASS",
                    "optional": false
                },
                {
                    "id": "async",
                    "name": "Async",
                    "type": "checkbox",
                    "variable": "IS_ASYNC"
                }
            ],
            "files": [ ... ]
        }
    ]
}

Files

Each entry in files describes one file to create.

FieldRequiredDescription
nameNoOutput file name. Supports template variables. Default: ${INPUT_CLASS|className}.php.
directoryNoTarget directory relative to the app root.
appNamespaceNoPHP namespace for the generated class.
baseClassNoFQN of the base class or interface.
fallbackBaseClassNoFQN used when baseClass is not found in the project.
templateNoTemplate configuration. See Template.

Template

FieldRequiredDescription
pathYesPath to the stub file.
fallbackPathNoAlternative stub path used when path is not found.
parametersNoMap of placeholders in the stub to template expressions.

The parameters map replaces literal strings in the stub file with dynamic values. Values support the template variable syntax ${VARIABLE_NAME}.

Template variables

The following variables are available in name, template.parameters values:

VariableDescription
${INPUT_CLASS}The raw input from the user (e.g. "Http/Controllers/UserController").
${INPUT_FQN}Fully-qualified class name derived from the input and directory settings.
${BASE_FQN}Short class name of the base class (the part after the last \).
${BASE_EXTENDS_STRING}" extends ClassName" or " implements InterfaceName", ready to embed in a class declaration.
${USES}use statement(s) for the base class, with surrounding newlines.
${YOUR_VARIABLE}Any variable declared in a parameters entry via the variable field.

See Template language for more information.

Full example

json
{
    "$schema": "https://laravel-ide.com/schema/laravel-ide-v2.json",
    "codeGenerations": [
        {
            "id": "me/myPackage:tenant-action",
            "name": "Create Tenant Action",
            "classSuffix": "Action",
            "parameters": [
                {
                    "id": "model-param",
                    "name": "Model",
                    "type": "eloquentClass",
                    "variable": "MODEL_CLASS",
                    "optional": true
                }
            ],
            "files": [
                {
                    "directory": "app/Actions",
                    "appNamespace": "App\\Actions",
                    "baseClass": "\\App\\Actions\\BaseAction",
                    "template": {
                        "path": "/stubs/tenant-action.stub",
                        "fallbackPath": "stubs/tenant-action.stub",
                        "parameters": {
                            "{{ namespace }}": "${INPUT_FQN|namespace}",
                            "{{ model }}": "${MODEL_CLASS|className}",
                            "{{ class }}": "${INPUT_FQN|className}",
                            "{{ extends }}": "${BASE_EXTENDS_STRING}",
                            "$$USES$$": "${USES}"
                        }
                    }
                }
            ]
        }
    ]
}

The stub file first will be searched for in the project root – "/stubs/tenant-action.stub". If not found, it will be searched by a relative path – "stubs/tenant-action.stub".

/stubs/tenant-action.stub:

php
<?php

namespace {{ namespace }};

$$USES$$

class {{ class }}{{ extends }}
{
    public function handle({{ model }} $model): void
    {
        //
    }
}