Luminova Framework

PHP Luminova: Build and Run a Blog Command

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

Create a routable blog command and run it from the terminal.

This example registers a blog command in the application router. Run its commands through php public/index.php from the project root. Use the Novakit command reference for commands registered directly with Novakit. See the Novakit Commands.


Creating a Simple Blog Application in CLI with a CLI Controller

This example demonstrates how to build a simple blog application using Luminova’s CLI AbstractController within a CLI environment.

Setting Up Command Routing

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.

Define the command routing for handling blog-related operations:

// /routes/cli.php
use Luminova\Routing\Router;

Router::group('blogs', static function () {
    Router::command("list", 'BlogCommand::list');
    Router::command('/read/id/(:value)', 'BlogCommand::read');
});

Creating the Controller Class for Blog Operations

The BlogCommand controller class handles the various blog operations:

// /app/Controllers/Cli/BlogCommand.php
namespace App\Controllers\Cli;

use Luminova\Command\Utils\Color;
use Luminova\Database\Query\Builder;
use Luminova\Base\Cli\AbstractController;

class BlogCommand extends AbstractController
{
    protected string $group = 'blogs';
    protected string $name  = 'blog-command';
    /**
     * @var array<int|string, string>
     */
    protected array $usages = [
        'php public/index.php blogs list' => 'Get a list of blogs',
        'php public/index.php blogs read id "foo"' => 'Read a blog by its ID',
    ];
    protected string $description = 'CLI tool for managing blogs on our website.';
    /**
     * @var array<string, mixed>
     */
    protected array $options = [];

    /**
     * Display help information for the blog command.
     *
     * @param array<string, mixed> $helps Help information for the command.
     * @return int Status code.
     */
    public function help(array $helps): int
    {
        return STATUS_ERROR;
    }

    /**
     * List all available blogs.
     *
     * @return int Status code.
     */
    public function list(): int
    {  
        // Retrieve blogs from the database
        $blogs = Builder::table('blogs')->select(['id', 'title'])->get();

        // Display header
        $this->write(Color::style('OUR BLOGS', 'red'));
        $this->newLine();

        // Display each blog
        foreach ($blogs as $row) {
            $this->writeln(Color::style('[' . $row->id . ']', 'green') . ' ' . $row->title);
        }

        return STATUS_SUCCESS;
    }

    /**
     * Display details of a specific blog.
     *
     * @param string $id Blog ID.
     * @return int Status code.
     */
    public function read(string $id): int
    {  
        // Retrieve the blog from the database
        $blog = Builder::table('blogs')
                              ->where('id', '=', $id)
                              ->find()->get();

        if (!$blog) {
            $this->writeln('Blog not found.');
            return STATUS_ERROR;
        }

        // Display blog details
        $this->write(Color::style('[' . $blog->title . ']', 'red'));
        $this->newLine();
        $this->writeln($blog->description);

        return STATUS_SUCCESS;
    }
}

Execution Example

To interact with your blog application via CLI, use the following commands:

  • List Blogs
    Retrieve a list of all blogs:

    php public/index.php blogs list
  • Read a Specific Blog
    Retrieve and display the details of a blog by its ID:

    php public/index.php blogs read id 123456

Run these commands from the project root, which contains the public/ directory.