Laravel: From Zero to CLI(ro)
Tags: laravel, php, cli, tooling
Published: 2024-01-22 08:00:00


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](https://github.com/nunomaduro/termwind) and [Laravel Zero](https://laravel-zero.com/).

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 
```bash
composer create-project --prefer-dist laravel-zero/laravel-zero project-name
```
After that, enter the project and run the command 
```bash
php project-name make:command CommandName
```
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.

```php
public function handle()
{
	render(<<<'HTML'
		<div>
			<div class="px-1 bg-green-600 py-10 w-full">
				<span> Mondev </span>
			</div>
			<em class="ml-1 py-10 text-red-500 font-bold">
				The Developer's Newsletter 0_1
			</em>
		</div>
	HTML);
}
```
Ther result will look something like this: 

![First result example](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/p4z50236t4xgeoo3yvcf.png)


Once you've achieved the desired result, you just need to run the command 
```bash
php project-name app:build build-name
```
to get a file ready to be executed with PHP in the `builds` folder! So, go into the folder and run in the terminal 
```bash
php build-name
```
And you'll see a classic helper with all the possible commands and functionalities!


![Laravel zero command helper](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/z07x5xr6zb6md6ueiymg.png)

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