Small but powerful CLI apps with Minicli

Published on by

Small but powerful CLI apps with Minicli image

Building CLI applications can be a lot of fun. We don't have to worry about the UI, and we can write beautiful PHP code that doesn't need any build steps.

When building CLI applications in PHP, we aren't as spoilt for choice as in building web applications - but there are some solid contenders. From using the defacto standard Symfony Console component or the extra spicy Laravel Zero. However, when building a CLI application, you may want to be as dependency free as possible - which is where Minicli comes in. Minicli was released a while ago by Erika Heidi as an experiment to build a dependency-free CLI framework that leaned on PHPs readline extension as its only dependency.

I have been spending a lot of time investigating CLI options for a project I am working on. At first, I started with my usual choice: Laravel Zero. It is familiar to me and any other developer who knows Laravel. Then I started questioning the portability aspect and requiring PHP for people who want to use it. This is for work, and not all of our users have PHP installed. So I dived into the world of compiled languages, looking at both GoLang and Rust. While there aren't many options available, the options out there are excellent.

There has been a lot of talk about native PHP recently, which drew my attention back toward the PHP space. What if I could build a lightweight, easy-to-maintain, and portable PHP CLI application? This was when I took another look at Minicli. Having played with it when it first came out, I was impressed with how nice it was to use for something dependency free - but also how easy it was to get started!

The recommended approach for building a Minicli application is to use the application skeleton and composer to set everything up and ready for you.

composer create-project --prefer-dist minicli/application my-awesome-idea

The directories should be familiar enough for you as Laravel developers, having an app directory and namespace. The commands you can create are recommended to be built as Command Controllers, which are class-based commands.

We create a command namespace under app/Commands, where you keep your commands.

mkdir app/Commands/LaravelNews

Under each namespace, you can add multiple commands for different variations. It is inferred that if no arguments are passed, you will want to use the DefaultController. Let's have a look at how to create a command.

declare(strict_types=1);
 
namespace App\Command\LaravelNews;
 
use Minicli\Command\CommandController;
 
final class DefaultController extends CommandController
{
public function handle(): void
{
$this->getPrinter()->display("Laravel News rocks");
}
}

Each command controller must be handled and doesn't have to return anything - unlike in Symfony or Laravel Zero, where an exit code is expected. To interact with output, you get the printer - and ask it to output something.

So, if we want to add an alternative version - we can create another command controller in our namespace.

declare(strict_types=1);
 
namespace App\Command\LaravelNews;
 
use Minicli\Command\CommandController;
 
final class InfoController extends CommandController
{
public function handle(): void
{
$this->getPrinter()->info("Laravel News rocks");
}
}

Now we can call our command:

./minicli laravel-news info

Which will give a different view to the default command, and we can use the following other options:

display(): A simple text output. info(): An informative text output. error(): An error formatted text output. success(): A success formatted text output.

Each option accepts a second argument as alt for alternative output, which will do a block color output with writing instead of colored writing.

It isn't as pretty as something like Laravel Zero using Termwind - but sometimes you don't need pretty!

Usually, when building a CLI application, we want to interact with a third-party API or another service to perform an action or logic. In Minicli, this is done by creating services.

// minicli
$app = new App();
$app->registerService(
'email',
new MyEmailImplementation(),
);

Then within our commands, we can get the app instance and call our service directly:

public function handle(): void
{
$service = $this->getApp()->email;
 
try {
$service->send(new EmailTemplate());
} catch (Throwable $exception) {
$this->getPrinter()->error($exception->getMessage());
}
}

So we have a lightweight, powerful CLI framework that we can leverage to aid in our development workflow - that has no dependencies allowing us to write beautiful PHP.

Have you tried Minicli yet? Have you used anything similar? Let us know on twitter!

Steve McDougall photo

Educator and Content creator, freelance consultant, API evangelist

Cube

Laravel Newsletter

Join 40k+ other developers and never miss out on new tips, tutorials, and more.

image
Bacancy

Outsource a dedicated Laravel developer for $2,500/month. With over a decade of experience in Laravel development, we deliver fast, high-quality, and cost-effective solutions at affordable rates.

Visit Bacancy
Curotec logo

Curotec

World class Laravel experts with GenAI dev skills. LATAM-based, embedded engineers that ship fast, communicate clearly, and elevate your product. No bloat, no BS.

Curotec
Bacancy logo

Bacancy

Supercharge your project with a seasoned Laravel developer with 4-6 years of experience for just $2500/month. Get 160 hours of dedicated expertise & a risk-free 15-day trial. Schedule a call now!

Bacancy
Laravel Forge logo

Laravel Forge

Easily create and manage your servers and deploy your Laravel applications in seconds.

Laravel Forge
Tinkerwell logo

Tinkerwell

The must-have code runner for Laravel developers. Tinker with AI, autocompletion and instant feedback on local and production environments.

Tinkerwell
Cut PHP Code Review Time & Bugs into Half with CodeRabbit logo

Cut PHP Code Review Time & Bugs into Half with CodeRabbit

CodeRabbit is an AI-powered code review tool that specializes in PHP and Laravel, running PHPStan and offering automated PR analysis, security checks, and custom review features while remaining free for open-source projects.

Cut PHP Code Review Time & Bugs into Half with CodeRabbit
Join the Mastering Laravel community logo

Join the Mastering Laravel community

Connect with experienced developers in a friendly, noise-free environment. Get insights, share ideas, and find support for your coding challenges. Join us today and elevate your Laravel skills!

Join the Mastering Laravel community
Kirschbaum logo

Kirschbaum

Providing innovation and stability to ensure your web application succeeds.

Kirschbaum
Shift logo

Shift

Running an old Laravel version? Instant, automated Laravel upgrades and code modernization to keep your applications fresh.

Shift
Lucky Media logo

Lucky Media

Get Lucky Now - the ideal choice for Laravel Development, with over a decade of experience!

Lucky Media
Lunar: Laravel E-Commerce logo

Lunar: Laravel E-Commerce

E-Commerce for Laravel. An open-source package that brings the power of modern headless e-commerce functionality to Laravel.

Lunar: Laravel E-Commerce
LaraJobs logo

LaraJobs

The official Laravel job board

LaraJobs
SaaSykit: Laravel SaaS Starter Kit logo

SaaSykit: Laravel SaaS Starter Kit

SaaSykit is a Multi-tenant Laravel SaaS Starter Kit that comes with all features required to run a modern SaaS. Payments, Beautiful Checkout, Admin Panel, User dashboard, Auth, Ready Components, Stats, Blog, Docs and more.

SaaSykit: Laravel SaaS Starter Kit
MongoDB logo

MongoDB

Enhance your PHP applications with the powerful integration of MongoDB and Laravel, empowering developers to build applications with ease and efficiency. Support transactional, search, analytics and mobile use cases while using the familiar Eloquent APIs. Discover how MongoDB's flexible, modern database can transform your Laravel applications.

MongoDB

The latest

View all →
Laravel Simple RabbitMQ Package image

Laravel Simple RabbitMQ Package

Read article
Navigating Dates Elegantly with Carbon in Laravel image

Navigating Dates Elegantly with Carbon in Laravel

Read article
Memoized Cache Driver in Laravel 12.9 image

Memoized Cache Driver in Laravel 12.9

Read article
Laravel Cookie Consent image

Laravel Cookie Consent

Read article
Converting Non-Decimal Strings with Laravel's Enhanced toInteger() Method image

Converting Non-Decimal Strings with Laravel's Enhanced toInteger() Method

Read article
Herd Xdebug Toggler for Visual Studio Code image

Herd Xdebug Toggler for Visual Studio Code

Read article