PHP Luminova: Route CLI Commands to Luminova Controllers
Register command groups and handlers, capture arguments, and apply CLI middleware.
A routable CLI command starts with a group name followed by the command name and any arguments. Run the examples from your project root with php public/index.php. Choose attribute routing or method-based routing for the examples you follow. See the CLI Command Routing.
Examples
Method-Based Routes
Current source limitation: Grouped CLI dispatch can fail in the router before the command handler runs. The examples below show how to register the routes.
When you are not using attribute routing, you define your CLI prefixes in /public/index.php.
Example:
The following setup maps any request CLI prefix for all command:
// /public/index.php
use Luminova\Boot;
use Luminova\Routing\Prefix;
Boot::http()->router->context([
new Prefix(Prefix::CLI, isCommand: true),
// Other routes
])->run();Note:Do not remove your website prefixes, just add cli prefix if not already added.
Route context file
For example, the cli prefix above corresponds to /routes/cli.php.Inside this file, you define all routes for the CLI context using the static methods on Router.
// /routes/cli.php
use Luminova\Routing\Router;
Router::group('demo', static function(){
Router::command('hello', 'Command::hello');
});Execute:
php public/index.php demo helloAttribute-Based Routes
When attribute routing is enabled, you no longer need to manually register CLI prefix in /public/index.php.Instead, you define them directly inside your controller class using the #[Group] attribute.
Controller Example:
The following controller automatically handles all command related to group (except demo).
// /app/Controllers/Cli/Command.php
namespace App\Controllers\Cli;
use Luminova\Attributes\Group;
use Luminova\Base\Cli\AbstractController;
#[Group(name: 'demo')]
class Command extends AbstractController
{
public function help(array $helps): int
{
return STATUS_ERROR;
}
// Define controller methods here
}Same as HTTP routing, with attribute routing, your index.php setup stays clean because the router automatically reads prefixes from attributes:
// /public/index.php
use Luminova\Boot;
Boot::http()->router->context()->run();Middlewares
Middleware allows you to intercept incoming commands before they reach your controllers.This is useful for tasks like authentication, access control, or pre-processing.
If middleware fails (returns an error), the controller will never be called — instead, the defined error handler will run.
Middleware should return:
STATUS_SUCCESS(allow request to proceed)STATUS_ERROR(block request and trigger error)
Using middleware with a route attribute
In this example, we use session authentication.We assume that the session object is available as $this->app->session inside your App\Application class.
// /app/Controllers/Cli/Controller.php
use Luminova\Attributes\Command;
#[Command(group: 'demo', middleware: Command::GROUP_MIDDLEWARE)]
public function middleware(): int
{
if ($this->app->session->isOnline()) {
return STATUS_SUCCESS;
}
return STATUS_ERROR;
}Using middleware with method-based routing
If you’re not using attributes, you can attach middleware directly in your route file.
In CLI, middlewares are define using guard:
// /routes/cli.php
use Luminova\Routing\Router;
Router::group('demo', function(Application $app){
Router::guard('demo', 'Command::middleware');
Router::command('list', 'Command::list');
Router::command('list/limit/(:int)', 'Command::list');
});Execute:
Running below command will trigger middleware authentication first before command is executed.
php public/index.php demo list
php public/index.php demo list limit=10Handling Command
Luminova uses the Luminova\Command\Terminal class for all CLI related helper functions. All method are static making it easy to access. But before calling method ensure you initialize by calling Terminal::init() or use object new Terminal().Once initialized you call the methods (e.g, Terminal::write()) to handle command.
Here’s how to set up a simple command route.
Handling commands with an attribute
Define a controller method with a #[Route] attribute:
// /app/Controllers/Http/Controller.php
use Luminova\Attributes\Command;
#[Command('message', group: 'demo')]
public function message(): int
{
$message = $this->option('message', 'm');
$this->write($message);
return STATUS_SUCCESS;
}Execute:
php public/index.php demo message="Hello world!"Handling commands with method
You can also handle commands inside a route context file such as /routes/cli.php.
Using a controller method handler:
// /routes/cli.php
use Luminova\Routing\Router;
Router::group('demo', function(){
Router::command('message', 'Command::message');
});Execute:
php public/index.php demo message="Hello world!"Dynamic CLI segments
You can capture parts of the command if you choose not to use arguments, the capture is similar to URL using patterns, named placeholders, or predefined placeholder patterns.
This allows your routes to handle Commands like php public/index.php demo user=peter1 dynamically without using arguments (e.g, php public/index.php demo --user=peter1).
Arguments may be better choice as they are tied to controller $options property making it visible when help command is run and they also support alias (e.g --foo, -f).
Capturing segments with an attribute
// /app/Controllers/Cli/Command.php
use Luminova\Attributes\Command;
#[Command('/user/(:username)', group: 'demo')]
public function profile(string $username): int
{
$this->write($username);
return STATUS_SUCCESS;
}Execute:
php public/index.php demo user username="peter"Capturing segments with method-based routing
// /routes/cli.php
use Luminova\Routing\Router;
Router::group('demo', function(){
Router::command('/user/(:username)', 'Command::profile');
});These examples capture a dynamic username from the Command and pass it to the controller method to process the user profile.
Uploading Files
When processing form submissions or API calls (e.g., updating a user profile via POST), it’s best to use a dedicated controller instead of mixing logic into your main web controller.
// /app/Controllers/Cli/Command.php
use App\Models\User;
use Luminova\Attributes\Command;
#[Command('/upload', group: 'demo')]
public function upload(): int
{
$file = $this->option('file', 'f');
$destination = $this->option('destination', 'd');
$name = $this->option('name', 'n', null);
$message = 'Failed to upload file';
if($this->upload($file, $destination, $name)){
$message = 'File was uploaded';
}
$this->success($message);
return STATUS_SUCCESS;
}Execute:
php public/index.php demo upload --file="path/to/image.png" --destination="path/to/assets/images/"Nested binding and Aliases
You can group related routes together under a shared command segment.For example, if you want to handle /about and want an aliases /info, you can bind them using a closure or define them directly in a controller with attributes.
Route Aliases (Redirects or Alternate Paths)**
Show how to point multiple commands to the same controller method — useful for legacy routes or SEO.
#[Command('about', group: 'demo', aliases: ['info'])]
public function about(): int
{
$about = $this->systemInfo();
var_export($about);
return STATUS_SUCCESS;
}Execute:
php public/index.php demo about
php public/index.php demo infoStack multiple route attributes
Define more than one #[Route] attribute on the same method:
// /app/Controllers/Cli/Command.php
use App\Models\User;
use Luminova\Attributes\Command;
#[Command('about', group: 'demo')]
#[Command('info', group: 'demo')]
public function about(): int
{
$about = $this->systemInfo();
var_export($about);
return STATUS_SUCCESS;
}Using method-based routing with bind
You can bind all about related commands to a single block in your route file:
// /routes/web.php
use App\Application;
use Luminova\Routing\Router;
Router::group('demo', function(){
Router::command('/about', 'Command::about');
Router::command('/info', 'Command::about');
});CLI Session
This example demonstrates how to maintain a persistent login session in CLI applications using the Luminova Session Class.It allows commands to store and retrieve user state (like login information) between executions.
// /app/Controllers/Cli/CommandController.php
namespace App\Controllers\Cli;
use Luminova\Sessions\Session;
use Luminova\Command\Utils\Color;
use Luminova\Base\Cli\AbstractController;
class CommandController extends AbstractController
{
private ?string $sid = null;
private ?Session $session = null;
/**
* Called when the command is initialized.
*/
protected function onCreate(): void
{
$this->sid = Session::toSessionId($this->getSystemId());
$this->session = new Session();
$this->session->setStorage('cli');
if (!$this->session->start($this->sid)) {
$this->error('Unable to start session in CLI');
}
}
/**
* Display help message.
*/
public function help(array $helps): int
{
return STATUS_ERROR;
}
public function login(): int
{
if (!$this->session->isStarted()) {
$this->error('Cannot use session in CLI');
return STATUS_SUCCESS;
}
if ($this->session->isOnline()) {
$this->write(sprintf(
"User is already online.\nUsername: %s\nLast Login: %s\nSID: %s",
$this->session->get('username'),
$this->session->get('date'),
$this->session->getId()
));
return STATUS_SUCCESS;
}
$username = $this->input('Enter your username to login: ');
if (!$username) {
$this->error('Username is required');
return STATUS_SUCCESS;
}
$date = date('d-m-Y H:i:s A');
$this->session->put('username', $username);
$this->session->put('date', $date);
if (!$this->session->login()) {
$this->error('User login failed', 'red');
return STATUS_SUCCESS;
}
$this->write(sprintf(
"Login successful.\nUsername: %s\nLogin Date: %s\nSID: %s",
$username,
$date,
$this->sid
));
return STATUS_SUCCESS;
}
public function logout(): int
{
if (!$this->session->isStarted()) {
$this->error('Cannot use session in CLI');
return STATUS_SUCCESS;
}
if ($this->session->isOnline()) {
$username = $this->session->get('username');
$this->session->logout();
$this->success(sprintf(
"User '%s' has successfully logged out.",
$username
));
return STATUS_SUCCESS;
}
$this->write('User is already logged out', 'yellow');
return STATUS_SUCCESS;
}
}Register Session Handlers
To register the above commands in your CLI routes:
// /routes/cli.php
use Luminova\Routing\Router;
Router::group('demo', static function (): void {
Router::command('login', 'CommandController::login');
Router::command('logout', 'CommandController::logout');
});Execute:
php public/index.php demo login
php public/index.php demo logout