Luminova Framework

PHP Luminova: Command Attributes for CLI Controller Methods

Last updated: 2026-11-28 20:57:12

Map CLI controller methods to command patterns, aliases, and middleware with the Command attribute.

Apply #[Command] to a public method in a routable CLI controller. Set a default group on the class with Group, or specify group on a command. A method can have multiple Command attributes. See CLI command routing for route setup and execution.

Examples

Register a Command

namespace App\Controllers\Cli;

use Luminova\Attributes\Command;
use Luminova\Attributes\Group;
use Luminova\Base\Cli\AbstractController;

#[Group('foo')]
class CommandController extends AbstractController
{
    protected string $group = 'foo';

    public function help(array $helps): int
    {
        return STATUS_ERROR;
    }

    #[Command('bar', aliases: ['list'])]
    public function barMethod(): int
    {
        return STATUS_SUCCESS;
    }
}

Run php index.php foo bar or php index.php foo list from the public directory.

The equivalent method-based route uses Router::group() and Router::command().

use Luminova\Routing\Router;

Router::group('foo', static function (): void {
    Router::command('bar', 'CommandController::barMethod');
    Router::command('list', 'CommandController::barMethod');
});

Group Middleware

#[Command(group: 'foo', middleware: Command::GROUP_MIDDLEWARE)]
public function guardFoo(): int
{
    return STATUS_SUCCESS;
}

The equivalent method-based registration uses Router::guard('foo', 'CommandController::guardFoo') within the group.

Global Middleware

#[Command(group: 'foo', middleware: Command::GLOBAL_MIDDLEWARE)]
public function guardCommands(): int
{
    return STATUS_SUCCESS;
}

The equivalent method-based registration uses Router::guard('global', 'CommandController::guardCommands').

Class Definition

  • Full namespace: Luminova\Attributes\Command
  • This class is final. The attribute targets methods and is repeatable.

Constants

NameValueDescription
GROUP_MIDDLEWAREguardRun middleware for the selected command group.
GLOBAL_MIDDLEWAREglobalRegister middleware for CLI commands globally.

Properties

aliases

Additional command patterns. Inherit the same handler by supplying aliases on a command registration.

/**
 * @var string[]|null
 */
public ?array $aliases = null;

Methods

__construct

Define a CLI command or middleware registration.

public function __construct(
    string $pattern = '/',
    ?string $group = null,
    bool $isErrorHandler = false,
    ?string $middleware = null,
    ?array $aliases = null,
)

Parameters:

ParameterTypeDescription
$patternstringCommand pattern, such as list or list/limit/(:int) (default /).
$groupstring|nullGroup override. The default null uses the class’s Group attribute.
$isErrorHandlerboolKeep the default false for command registration.
$middlewarestring|nullMiddleware type. Use one of the middleware constants or keep the default null for a command handler.
$aliasesstring[]|nullAdditional command patterns for the same handler (default null).

Throws: