Movatterモバイル変換


[0]ホーム

URL:


Skip to content
DEV Community
Log in Create account

DEV Community

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

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 andLaravel 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 thebuild 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 modeExit fullscreen mode

After that, enter the project and run the command

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

This way, similar to what happens withartisan, you'll have a new command generated inapp/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.

publicfunctionhandle(){render(<<<'HTML'        <div>            <div>                <span> Mondev </span>            </div>            <em>                The Developer's Newsletter 0_1            </em>        </div>    HTML);}
Enter fullscreen modeExit 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 modeExit fullscreen mode

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

php build-name
Enter fullscreen modeExit 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)

Subscribe
pic
Create template

Templates let you quickly answer FAQs or store snippets for re-use.

Dismiss

Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment'spermalink.

For further actions, you may consider blocking this person and/orreporting abuse

Hi, I'm Giuliano and I'm a full stack web developer based in Rome. I love playing around with code trying out new things. I love to share knowledge and get into inspiring conversations.
  • Location
    Rome, Italy
  • Pronouns
    he/him
  • Work
    Full-stack web developer and dev mentor
  • Joined

More fromGiuliano1993

DEV Community

We're a place where coders share, stay up-to-date and grow their careers.

Log in Create account

[8]ページ先頭

©2009-2025 Movatter.jp