6 min read

Laravel Zero: Micro Framework for Simplified CLI App Development

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.

Why Choose Laravel Zero?

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.

Key Features of Laravel Zero

Here are the standout features that make Laravel Zero a great choice for CLI app development:

  • Simplicity and Speed: Laravel Zero is intended to be practical with minimal setup. This allows developers to create functional scripts without worrying about configurations.
  • Flexibility: It features an adaptable structure that enables components to be added or removed as needed, making it perfect for adapting the framework to satisfy the requirements of a particular job.
  • Integration with Laravel Components: While it is a micro-framework, it is functional with the majority of Laravel components and provides Laravel developers with an enjoyable place to work.

Laravel Zero: Real-World Applications

Some of the practical use cases of Laravel Zero include the following:

  • Automating Data Migrations and Backups: The command syntax is simple yet powerful, helping it to automate complex data transfer and backup tasks.
  • Task Scheduling: Laravel Zero handles scheduled tasks properly, whether they include tidying up databases or sending out email notifications.
  • Implementing Deployment Scripts: Through automation and automating their deployment methods, developers can reduce the risk and time spent on manual deployments.

Implementing Laravel Zero Into a QuizApp

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.

Create Laravel Zero Project

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

Set Project Name

Then we need to run the app:rename command to rename the project:

php application app:rename quiz

Create a File to Store Quiz Questions

After this, we can start creating our projects.

mkdir storage/json

And create quizzes.json file. 

Create Commands

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']));
    }
}

Execute Commands

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.

Meet the authors

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.

Contact us