Jack Miras
Posted on November 6, 2023
When working with Lumen applications, every developer anticipates that the framework will have fewer native features when compared with Laravel. Different from its counterpart, which is intended to build full-featured applications, Lumen is intended for building smaller, faster microservices and lightweight APIs.
One thing not present in Lumen that I find hard to work without is a REPL. Even though the framework has fewer features than Laravel, it still offers database, cache, events, ques, and so on, but believe it or not, Lumen doesn't offer a REPL out of the box.
Fortunately for us, the process of making a REPL available to a Lumen application is not complex, and we can achieve it with a few steps.
Content
What is a REPL?
Before we start, let's explore what a REPL is. The acronym stands for Read-Eval-Print Loop and allows developers to enter commands or code snippets, which are then read, evaluated, and have their results printed back to the developer. This loop continues, making it an excellent tool for quickly experimenting with code, testing small pieces of code, and learning new programming languages or libraries.
Tinker is a REPL built over psysh which is a general-purpose REPL for PHP. Since a Laravel application is not plain PHP and has states, databases, caches, events, and many more complex resources specific to the framework, it's not possible to interact with the application the way one would expect just using psysh.
And that's where Tinker comes into play, since it's a psysh integration that implements all the specifics necessary for a developer to interact with Laravel in a REPL manner, no matter the resource or feature of the framework being accessed in the REPL.
If you are new to Lumen, you might wonder: Since Tinker is a Laravel REPL, why are we looking into it for a Lumen application since they are entirely different frameworks? Well, you are right; as the Lumen documentation states, they are in fact two different frameworks with intentional incompatibilities with libraries. However, Lumen was born out of Laravel, and it shares some of its foundations with its older sibling, which in some cases means we can use Laravel dependencies in Lumen, and Tinker is one of them.
Require dependency
Now that we have an understanding of a REPL and Tinker's foundations, let's, as a first step, add Tinker as a dependency to our project. Below is the command you have to run to add Tinker as a development dependency to Lumen.
composer require --dev laravel/tinker
Define service provider
The Tinker dependency was added to the project; the next step is to register Tinker's service provider at bootstrap/app.php
. In Lumen, you typically register service providers in the bootstrap/app.php
file to enable various functionalities within your application.
A service provider is a way to bind classes into the service container, configure services, and perform other setup tasks. Registering Tinker's service provider in the bootstrap/app.php
file is necessary to integrate the Tinker REPL into your Lumen application. By registering its service provider, you're telling Lumen to make Tinker's REPL available to you when you run the php artisan tinker
command.
$app->register(Laravel\Tinker\TinkerServiceProvider::class);
Run the tinker command
Once the service provider is registered and Lumen has made Tinker's REPL available, you can run php artisan tinker
and interact with any Lumen component you need.
Now your Lumen application has a REPL configured for it.
Happy coding!
Posted on November 6, 2023
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.