Skip to content
Edit this page

CLI Generators

CodeIgniter4 now comes equipped with generators to ease the creation of stock controllers, models, entities, etc. You can also scaffold a complete set of files with just one command.

Introduction

All built-in generators reside under the Generators group when listed using php spark list. To view the full description and usage information on a particular generator, use the command:

php spark help <generator_command>
php spark <generator_command> --help

where <generator_command> will be replaced with the command to check.

Note

The single-letter option shortcuts listed below (-n, -s, -f) are available since v4.8.0.

Note

Do you need to have the generated code in a subfolder? If you want to create a controller class to reside in the Admin subfolder of the main Controllers folder, you will just need to prepend the subfolder to the class name, like this: php spark make:controller admin/login. This command will create the Login controller in the Controllers/Admin subfolder with a namespace of App\Controllers\Admin.

Note

Working on modules? Code generation will set the root namespace to a default of APP_NAMESPACE. Should you need to have the generated code elsewhere in your module namespace, make sure to set the --namespace option in your command, e.g., php spark make:model blog --namespace Acme\\Blog.

Warning

Make sure when setting the --namespace option that the supplied namespace is a valid namespace defined in your $psr4 array in Config\Autoload or defined in your composer autoload file. Otherwise, code generation will be interrupted.

Built-in Generators

CodeIgniter4 ships the following generators by default.

make:cell

Available from version 4.3.0

Creates a new Cell file and its view.

Usage:

make:cell <name> [options]

Argument:

  • name: The name of the cell class. It should be in PascalCase. [REQUIRED]

Options:

  • --namespace (-n): Set the root namespace. Defaults to the value of APP_NAMESPACE.
  • --force (-f): Set this flag to overwrite existing files on destination.

make:command

Creates a new spark command.

Usage:

make:command <name> [options]

Argument:

  • name: The name of the command class. [REQUIRED]

Options:

  • --command (-c): The command name to run in spark. Defaults to command:name.
  • --type (-t): The type of command, whether a basic command or a generator command. Defaults to basic.
  • --group (-g): The group of the command. Defaults to App for basic commands, and Generators for generator commands.
  • --namespace (-n): Set the root namespace. Defaults to the value of APP_NAMESPACE.
  • --suffix (-s): Append the component suffix to the generated class name.
  • --force (-f): Set this flag to overwrite existing files on destination.

Note

Since v4.8.0, the generated class is a modern command: a basic command extends AbstractCommand, and a generator command extends AbstractGeneratorCommand.

make:config

Creates a new config file.

Usage:

make:config <name> [options]

Argument:

  • name: The name of the config class. [REQUIRED]

Options:

  • --namespace (-n): Set the root namespace. Defaults to the value of APP_NAMESPACE.
  • --suffix (-s): Append the component suffix to the generated class name.
  • --force (-f): Set this flag to overwrite existing files on destination.

make:controller

Creates a new controller file.

Usage:

make:controller <name> [options]

Argument:

  • name: The name of the controller class. [REQUIRED]

Options:

  • --bare (-b): Extends from CodeIgniter\Controller instead of BaseController.
  • --restful (-r): Extends from a RESTful resource. Choices are controller and presenter. Defaults to controller.
  • --namespace (-n): Set the root namespace. Defaults to the value of APP_NAMESPACE.
  • --suffix (-s): Append the component suffix to the generated class name.
  • --force (-f): Set this flag to overwrite existing files on destination.

Note

If you use --suffix, the generated controller name will be like ProductController. That violates the Controller naming convention when using Auto Routing (Controller class names MUST start with an uppercase letter and ONLY the first character can be uppercase). So --suffix can be used when you use Defined Routes.

make:entity

Creates a new entity file.

Usage:

make:entity <name> [options]

Argument:

  • name: The name of the entity class. [REQUIRED]

Options:

  • --namespace (-n): Set the root namespace. Defaults to the value of APP_NAMESPACE.
  • --suffix (-s): Append the component suffix to the generated class name.
  • --force (-f): Set this flag to overwrite existing files on destination.

make:filter

Creates a new filter file.

Usage:

make:filter <name> [options]

Argument:

  • name: The name of the filter class. [REQUIRED]

Options:

  • --namespace (-n): Set the root namespace. Defaults to the value of APP_NAMESPACE.
  • --suffix (-s): Append the component suffix to the generated class name.
  • --force (-f): Set this flag to overwrite existing files on destination.

make:model

Creates a new model file.

Usage:

make:model <name> [options]

Argument:

  • name: The name of the model class. [REQUIRED]

Options:

  • --table (-t): Supply a different table name. Defaults to the pluralized class name.
  • --dbgroup (-g): Database group to use. Defaults to default.
  • --return (-r): Set the return type from array, object, or entity. Defaults to array.
  • --namespace (-n): Set the root namespace. Defaults to the value of APP_NAMESPACE.
  • --suffix (-s): Append the component suffix to the generated class name.
  • --force (-f): Set this flag to overwrite existing files on destination.

make:request

Creates a new FormRequest file.

Usage:

make:request <name> [options]

Argument:

  • name: The name of the FormRequest class. [REQUIRED]

Options:

  • --namespace (-n): Set the root namespace. Defaults to the value of APP_NAMESPACE.
  • --suffix (-s): Append the component suffix to the generated class name.
  • --force (-f): Set this flag to overwrite existing files on destination.

make:seeder

Creates a new seeder file.

Usage:

make:seeder <name> [options]

Argument:

  • name: The name of the seeder class. [REQUIRED]

Options:

  • --namespace (-n): Set the root namespace. Defaults to the value of APP_NAMESPACE.
  • --suffix (-s): Append the component suffix to the generated class name.
  • --force (-f): Set this flag to overwrite existing files on destination.

make:test

Available from version 4.5.0

Creates a new test file.

Usage:

make:test <name> [options]

Argument:

  • name: The name of the test class. [REQUIRED]

Options:

  • --namespace (-n): Set the root namespace. Defaults to Tests.
  • --force (-f): Set this flag to overwrite existing files on destination.

make:transformer

Creates a new API transformer file.

Usage:

make:transformer <name> [options]

Argument:

  • name: The name of the transformer class. [REQUIRED]

Options:

  • --namespace (-n): Set the root namespace. Defaults to the value of APP_NAMESPACE.
  • --suffix (-s): Append the component suffix to the generated class name.
  • --force (-f): Set this flag to overwrite existing files on destination.

make:migration

Creates a new migration file.

Usage:

make:migration <name> [options]

Argument:

  • name: The name of the migration class. [REQUIRED]

Options:

  • --session: Generate a migration file for database sessions. The name argument is ignored.
  • --table (-t): Set the table name to use for database sessions. Defaults to ci_sessions.
  • --dbgroup (-g): Set the database group for database sessions. Defaults to default group.
  • --namespace (-n): Set the root namespace. Defaults to the value of APP_NAMESPACE.
  • --suffix (-s): Append the component suffix to the generated class name.

Note

make:migration has no --force option, since the timestamped file name only collides with an existing file when the same name is generated twice within one second.

make:validation

Creates a new validation file.

Usage:

make:validation <name> [options]

Argument:

  • name: The name of the validation class. [REQUIRED]

Options:

  • --namespace (-n): Set the root namespace. Defaults to the value of APP_NAMESPACE.
  • --suffix (-s): Append the component suffix to the generated class name.
  • --force (-f): Set this flag to overwrite existing files on destination.

Scaffolding a Complete Set of Stock Code

Sometimes in our development phase we create functionality in groups, such as an Admin group. This group will contain its own controller, model, migration files, or even entities. You may be tempted to type each generator command one by one in the terminal and wishfully think it would be great to have a single generator command to rule them all.

Fret no more! CodeIgniter4 is also shipped with a dedicated make:scaffold command that is basically a wrapper around the controller, model, entity, migration, and seeder generator commands. All you need is the class name that will be used to name all the generated classes. Also, individual options supported by each generator command are recognized by the scaffold command.

Running this in your terminal:

php spark make:scaffold user

will create the following files:

1) app/Controllers/User.php 2) app/Models/User.php 3) app/Database/Migrations/<some date here>_User.php and 4) app/Database/Seeds/User.php

To include an Entity class in the scaffolded files, just add --return entity to the command and it will be passed to the model generator.

Note

Since v4.8.0, make:scaffold accepts the -b, -t, -g, -n, -s, and -f shortcuts. --restful and --return have no shortcut here, since both use -r in their own commands. When the class name is omitted on an interactive run, it is asked for once and reused for every generated file.

GeneratorTrait

Deprecated since version 4.8.0

GeneratorTrait is the legacy way of building generator commands, tied to the deprecated BaseCommand. New generator commands should extend AbstractGeneratorCommand instead. See Modern Generator Commands.

Legacy generator commands use the GeneratorTrait to fully utilize its methods that are used in code generation.

Declaring the Location of a Custom Generator Command Template

The default order of lookup for generator templates is (1) the template defined in the app/Config/Generators.php file, and (2) if not found, the template found at the CodeIgniter\Commands\Generators\Views namespace.

To declare the template location for your custom generator command, you will need to add it to the app/Config/Generators.php file. For example, if you have a command make:awesome-command and your generator template is located within your app directory app/Commands/Generators/Views/awesomecommand.tpl.php, you would update the config file like so:

<?php

namespace Config;

use CodeIgniter\Config\BaseConfig;

class Generators extends BaseConfig
{
    public array $views = [
        // ..
        'make:awesome-command' => 'App\Commands\Generators\Views\awesomecommand.tpl.php',
    ];
}