DEV Community

Cover image for Laravel: From Zero to CLI(ro)

Posted on

Laravel: From Zero to CLI(ro)

Good morning, everyone, and happy MonDEV! ☕

This week we continue on the wave of CLI development, but this time in a completely different environment! You've heard me talk about command-line development with Python and Node; in the future, there will probably be something with Rust (just give me some time to get more familiar with it). But today, I want to go back to my roots, to what is my language of choice for web backend development.

Today, we're talking about developing CLI tools with PHP, specifically leveraging two very powerful tools made to work together: Termwind and Laravel Zero.

Starting with the first one, Termwind is a library developed by Nuno Maduro, allowing you to stylize your command-line outputs using Tailwind CSS classes! Sounds incredible? Yet it's true! Styling terminal content is always infernal if done natively. Having a tool that takes care of it for us saves a lot of time and results in a much more pleasant and usable display!

This library is already included by default in Laravel Zero, a minimal version of the beloved Laravel Web Framework but focused on command-line program development. Laravel Zero provides the classic Laravel APIs for writing commands but also offers some extra tools for distribution. Through the build command, as we'll see later, it allows you to generate a single phar file, executable using PHP, making the output of your work very easy to distribute! Few things are as beautiful as the ease of packaging the finished project and distributing it to the end user, don't you agree?

So, let's take a look at how to write our first command with Laravel Zero and make it beautiful with Termwind!

Create the project with

composer create-project --prefer-dist laravel-zero/laravel-zero project-name
Enter fullscreen mode Exit fullscreen mode

After that, enter the project and run the command

php project-name make:command CommandName
Enter fullscreen mode Exit fullscreen mode

This way, similar to what happens with artisan, you'll have a new command generated in app/Commands with all the basic structure of a Laravel command!

Now, if we play a bit with Termwind, we can start adding some style and get something that will look good.

public function handle()
            <div class="px-1 bg-green-600 py-10 w-full">
                <span> Mondev </span>
            <em class="ml-1 py-10 text-red-500 font-bold">
                The Developer's Newsletter 0_1
Enter fullscreen mode Exit fullscreen mode

Ther result will look something like this:

First result example

Once you've achieved the desired result, you just need to run the command

php project-name app:build build-name
Enter fullscreen mode Exit fullscreen mode

to get a file ready to be executed with PHP in the builds folder! So, go into the folder and run in the terminal

php build-name
Enter fullscreen mode Exit fullscreen mode

And you'll see a classic helper with all the possible commands and functionalities!

Laravel zero command helper

Easy, right?

Of course, we've only seen an extremely basic flow, and Laravel Zero offers many possibilities for this type of development, from database management to handling input, single or multiple-choice questions, to even some very nice add-ons like "Logo," which generates an ASCII art with the name of your application!

As always, the exploration is in your hands! I hope I've given you some interesting ideas and look forward to seeing some projects!

For now, as always, I just have to wish you a good week :D

Happy coding 0_1

Top comments (0)