Let’s Build a console app with Laravel Zero
Hey folks, today I will like to show you an pratical example of how to build an console application with Laravel Zero.
For those how don’t know, Laravel Zero is unofficial and customized version of Laravel optimised for building console/shell/command-line applications.
The core of Laravel Zero is a micro-framework that comes with only 3 Laravel PHP Components:
For more information, you can take a look on the github project: https://github.com/laravel-zero/laravel-zero.
On this article we are bulding a very simple application without showing the full pontencial and features of Laravel Zero.
Now, let’s make a Weather application, in order show the weather information of the current day.
Create a Laravel Zero application
composer create-project --prefer-dist laravel-zero/laravel-zero application
After the installation, go inside the application folder an run:
By default, your application will show an fancy message on your terminal plus an notification on your Operating System.
We need an HTTP client, in order to consume an Weather API, so let’s include the Adam Wathan Zttp.
composer require kitetail/zttp
Zttp is a developer-experience focused HTTP client, optimized for most common use cases. Allowing consuming an API super easily.
Build your command
Let’s go app/DefaultCommand.php and apply the following modifications:
Source code: https://github.com/nunomaduro/laravel-zero-weather
Finally, since your application is a weather application, let’s rename the executable:
php application rename weather
And thats it! Your application is now ready to be used!
Finally, like I said, this is a really simple example (not well coded). On next articles, I may talk about other features of Laravel Zero, for example:
- Support of Laravel 5 Service Providers
- Ships with a standalone compiler