PHP Luminova: Command Attributes for CLI Controller Methods
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
| Name | Value | Description |
|---|---|---|
GROUP_MIDDLEWARE | guard | Run middleware for the selected command group. |
GLOBAL_MIDDLEWARE | global | Register 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:
| Parameter | Type | Description |
|---|---|---|
$pattern | string | Command pattern, such as list or list/limit/(:int) (default /). |
$group | string|null | Group override. The default null uses the class’s Group attribute. |
$isErrorHandler | bool | Keep the default false for command registration. |
$middleware | string|null | Middleware type. Use one of the middleware constants or keep the default null for a command handler. |
$aliases | string[]|null | Additional command patterns for the same handler (default null). |
Throws:
- Luminova\Exceptions\RouterException when the middleware value is invalid.