Luminova Framework

PHP Luminova: CLI Command Controller (Routable Commands)

Last updated: 2026-09-02 11:17:45

Base command, Luminova's Framework for command-line operations - controller base class for NovaKit CLI Tool.

The Base Command class in Luminova allows you to build routable CLI tools with the same structure you use for HTTP controllers. It provides a clear way to define, organize, and execute custom commands within your application.

Luminova\Base\Command uses Luminova's routing system, allowing you to:

  • Define individual commands with the #[Route] attribute.
  • Group related commands with the class-level #[Group] attribute.

This is similar to how HTTP controllers work, but for command-line operations. Commands are executed via your front controller:

php /public/index.php <command>

Unlike Base Console, which handles lightweight commands directly using novakit, the Base Command is designed for full routing and tight integration with your application.

This approach makes it easy to add CLI features without additional learning, commands follow familiar controller patterns.


Features

  • Command Metadata Define commands with clear usages, options, and descriptions that display with --help.

  • Input Handling Automatically parse arguments, validate input, and execute the right method.

  • Command Grouping Organize commands logically using groups to keep your code clean.

  • Access Control Restrict commands to specific system users if needed.

  • Customizable Add custom handlers, define new options, or integrate third-party tools.

  • Reusability Use the same controller logic for CLI and HTTP requests, enabling smooth integration.

For formatting command output, use the Luminova Terminal helper.All methods are static and available in the CLI environment for command tasks.


Examples

Help Command

To print help information related to a specific novakit command.

php index.php <command> --help

Optional Help DisplayYou can create a custom help display or rely on the default formatting provided by Luminova.


Command Execution Pattern

Navigate to your project's public directory:

cd path/to/your-project/public

Run your command using this pattern:

php index.php <group-name> <command-name> <arguments>

To show help for a command group:

php index.php <group-name> --help

Notes:

  • The group-name comes first, followed by the command-name and any arguments.
  • Arguments can appear in any order after the command group.

Default Flags

Use these flags with commands to modify output formatting.

  • --no-header: Omits the header from the output.

    php index.php <command> --no-header
  • --no-color: Disables colored output.

    php index.php <command> --no-color
  • --no-ansi: Disables ANSI formatting for terminals that don't support it.

    php index.php <command> --no-ansi
  • --no-profiling: Disables showing performance profiling after command execution.

    php index.php <command> --no-profiling

Building Command-Line Tools

Creating CLI tools in Luminova is straightforward. Its routing system mirrors HTTP routing, so you can apply the same logic you already know to your command-line commands.


Command Controllers

To create a new command controller:

  1. Create Your Controller Place your classes in /app/Controllers/Cli/ and make sure they extend Luminova\Base\Command.

  2. Define Your Methods Implement commands using either PHP Attributes or code-based routing with the Router class.


Routing Command Controllers

Code-Based Routing Example

Define routes in /routes/cli.php:

use Luminova\Routing\Router;

Router::group('blog', static function(Router $router) {
   // Command without arguments
   Router::command('list', 'BlogCommand::blogs');

   // Command with a method argument
   Router::command('list/limit/(:int)', 'BlogCommand::blogs');
});

Attribute-Based Routing Example

Use PHP attributes directly in your command class:

namespace App\Controllers\Cli;

use App\Models\Blog;
use Luminova\Command\Terminal;
use Luminova\Attributes\Group;
use Luminova\Attributes\Command;

#[Group(name: 'blog')]
class BlogCommand extends Luminova\Base\Command
{
    protected string $group = 'blog';
    protected string $name  = 'blog-command';

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

    #[Command('list')]
    public function blogs(int $limit = 10): int 
    {
        $limit = $this->input->getAnyOption('limit', 'l', 10);

        Terminal::writeln("Limit: {$limit}");

        var_export(Blog::select(limit: $limit));

        return STATUS_SUCCESS;
    }

    #[Command('list/limit/(:int)')]
    public function blogs2(int $limit = 10): int 
    {
        Terminal::writeln("Limit: $limit");

        var_export(Blog::select(limit: $limit));

        return STATUS_SUCCESS;
    }
}

Note: Both routing methods produce the same results; use whichever style fits your project.


Executing Command

You can specify arguments for your commands in various ways. For example, to list blogs with a limit of 3:

cd path/to/project/public

php index.php blog list --limit=3 # Command with options
php index.php blog list limit=3   # Command with method argument

Displaying Command Help

To print help for the blog command, use the following command:

php index.php blog --help

Catching Exceptions

In the Luminova framework, exceptions in CLI operations are managed effectively. To enable exception handling, follow these guidelines:

Enable Exception Handling

Environment Configuration:

You can enable exception handling globally by setting the following option in your environment file:

throw.cli.exceptions = true

Temporary Enablement:

If you want to enable exception handling temporarily for the current script execution, you can use the following global function. Call it before your script runs or within the controller's __construct, onCreate, or any executing controller method:

setenv('throw.cli.exceptions', true);

Class Definition


Properties

allowDirectIncludes

Allows direct PHP include/require statements in this class. When enabled, the debugger skips include/require enforcement for the controller.

protected bool $allowDirectIncludes = false;

See the #[AllowDirectIncludes] class-level attribute for the attribute-based equivalent.


app

Lazy loaded application instance.

protected readonly Luminova\Interface\LazyObjectInterface $app;

This allows accessing, command arguments, options, flags or full executed command raw-string via the application instance.


input

Executed command object.

protected readonly Luminova\Command\Input $input;

This allows accessing, command arguments, options, flags or full executed command raw-string.

See the Command Input class documentation for details.


group

The group name for the current command controller class. This group is used for router capture authentication to ensure that commands within the same group are executed accordingly (e.g., php index.php <command-group-name> <command> <arguments>).

protected string $group = '';

name

The execution command name for the current controller class. This name is used internally to build information about your command, which can be utilized in displaying command help.

protected string $name = '';

usages

Command usage instructions. Keys represent the command usage, and values describe the usage. This can be either a string or an associative array of usage examples.

protected array<string|int,string>|string $usages = '';

options

Available command options. Keys represent the options, and values describe them (e.g., ['-f, --foo' => 'Foo description']).

protected array<string|int,string> $options = [];

examples

Examples demonstrating command usage.

protected array<string|int,string> $examples = [];

description

A detailed description of your command to help users understand its purpose and functionality.

protected string $description = '';

users

Defines a list of allowed system users who can execute the command. If the array is empty, all users are allowed by default.

protected array<int,string> $users = [];

Example:

Only the following users are permitted to run this command:

protected array<int,string> $users = ['www-data', 'ubuntu', 'admin'];

Note: This is useful for restricting sensitive commands to specific system-level users.


authentication

The authentication property allows you to configure login protection for CLI commands using either passwords or public/private key authentication.

Supported Array Keys / Database Column Names:

  • auth (string) – Type of authentication, such as password or key.
  • content (string) – The hashed password (for password login), public key content, or path to a public key file. Leave empty for public access without a password.
  • updated_at (datetime) – Records the last time authentication was used or updated.
protected ?array<string,