Interested in generating passive income? Join our partnership program and receive a commission on each new client referral. Learn more.
6 min read
Interested in generating passive income? Join our partnership program and receive a commission on each new client referral. Learn more.
Nuno Maduro and Owen Voke just released Laravel Zero, a simplified version of Laravel created specifically to build CLI applications. It uses the core features of Laravel but in a more simplified and accessible format. This approach helps developers to easily set up and manage processes like data handling scripts and automated jobs that are mainly managed from the command line.
This article takes you through the essential aspects of Laravel Zero and include a practical example to demonstrate how to implement it in an actual Laravel project.
When developing command-line interface (CLI) applications, efficiency and specialization are the most essential factors. Laravel Zero perfectly embodies these principles. This micro-framework, which originates from Laravel, is an ideal choice for developers who want to use refined CLI tools without dealing with the overhead of full-stack capabilities. It reduces down to the essentials.
Here are the standout features that make Laravel Zero a great choice for CLI app development:
Some of the practical use cases of Laravel Zero include the following:
To implement Laravel Zero in a real-life project, let’s create a new simple quiz application where users can see all the available quizzes and give opportunities to take them.
First, we should create a new Laravel Zero application:
composer create-project --prefer-dist laravel-zero/laravel-zero quiz-app
Navigate to the project:
cd quiz-app
Then we need to run the app:rename command to rename the project:
php application app:rename quiz
After this, we can start creating our projects.
mkdir storage/json
And create quizzes.json file.
First, we should create a command that will display all the available quizzes:
php quiz make:command ListQuizzes
<?php
namespace App\Commands;
use Illuminate\Console\Scheduling\Schedule;
use Illuminate\Support\Facades\File;
use LaravelZero\Framework\Commands\Command;
class ListQuizzes extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'app:list-quizzes';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Command description';
/**
* Execute the console command.
*/
public function handle()
{
$quizzesJson = File::get(storage_path('json/quizzes.json'));
$quizzes = json_decode($quizzesJson, true);
$this->info("Available Quizzes:");
foreach ($quizzes['quizzes'] as $quiz) {
$this->line("{$quiz['id']}: {$quiz['title']}");
}
}
}
The next command is taking the quiz by ID and answering the questions. For that, let’s create another command:
php quiz make:command TakeQuiz
<?php
namespace App\Commands;
use Illuminate\Console\Scheduling\Schedule;
use Illuminate\Support\Facades\File;
use LaravelZero\Framework\Commands\Command;
class TakeQuiz extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'app:take-quiz {id}';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Command description';
/**
* Execute the console command.
*/
public function handle()
{
$quizId = $this->argument('id');
$quizzesJson = File::get(storage_path('json/quizzes.json'));
$quizzes = json_decode($quizzesJson, true);
$quiz = collect($quizzes['quizzes'])->firstWhere('id', $quizId);
if (!$quiz) {
$this->error("Quiz not found.");
return;
}
$this->info("Starting quiz: {$quiz['title']}");
$score = 0;
foreach ($quiz['questions'] as $question) {
$answer = $this->choice($question['question'], $question['options']);
if ($answer === $question['answer']) {
$score++;
}
}
$this->info("Your score: $score/" . count($quiz['questions']));
}
}
If we run the following command, we’ll see available quizzes:
php quiz app:list-quizzes
Choose one option from the available quizzes:
php quiz app:take-quiz 2
So, with Laravel Zero’s tidy, understandable code structures, developers can build commands that handle specific tasks, like listing quizzes or taking a quiz, as shown in our example. This helps scalability and maintenance and enhances the development experience.
Here’s a link to the repository.
For more in-depth information, you can check the official documentation of Laravel Zero.
We are a 200+ people agency and provide product design, software development, and creative growth marketing services to companies ranging from fresh startups to established enterprises. Our work has earned us 100+ international awards, partnerships with Laravel, Vue, Meta, and Google, and the title of Georgia’s agency of the year in 2019 and 2021.