Diving into Lumen – the stunningly fast micro-framework by Laravel
Lumen is a project from Laravel creator Taylor Otwell introduced a couple of years ago. It’s a “micro-framework”, meaning it’s a smaller, faster, leaner version of a full web application framework. The main idea behind Lumen’s creation is that it is built for speed, and is one of the fastest PHP micro-frameworks available – even significantly faster than similar frameworks such as Silex and Slim.
It is important to note that Lumen is not designed to replace Laravel, rather, it is a more specialized (and stripped-down) framework designed for micro-services and APIs. It took away unneeded features for an API such as HTTP sessions and cookies, and also limited the number of configuration options. Out-of-the-box, Lumen sacrificed the flexibility of Laravel for speed. Its stunningly fast speed, combined with the convenience of Laravel’s features gives you a “best of both worlds” micro-framework that is truly a joy to work with.
Where did the name Lumen come from?
The name “Lumen” is a play on the “Illuminate” components that Laravel utilizes and it also continues the tradition of “light” based names. The Illuminate components are the individual pieces of the Laravel framework such as database, cache, queue, etc.
What is Lumen for?
Lumen has the same foundation as Laravel, and glues together most of the same top components (3rd party and Laravel’s own components). As such, front end niceties like Bootstrap and Elixir and the authentication bootstrap and sessions don’t come enabled out of the box, and there’s less flexibility for extending and changing the bootstrap files. However, unlike many other micro-frameworks, Lumen lets you tap into the full power of Laravel’s features, such as routing, dependency injection, the Eloquent ORM, migrations, queued jobs, and even scheduled commands.
But Lumen is built for micro services and fast REST API’s, not so much for user-facing applications (although it can be used for anything.) That being said, Lumen is for projects and components that can benefit from the convenience and power of Laravel but can afford to sacrifice some configurability and flexibility in exchange for a speed boost.
When Should I Use Lumen?
Lumen is targeted at micro services – small, loosely-coupled components that usually support and enhance a core project, so it’s more likely to be used when you’re separating out a single high-use piece of your application. For example, if there is one aspect of your Laravel application that receives drastically more traffic than the rest of the application, you may choose to build that aspect of the application as a small, separate Lumen application. So in a microservice architecture, you might have several small Lumen apps that support another, possibly Laravel-powered, app. By reducing the load on your primary Laravel application, you can cut server costs since applications built on Lumen do not require as much server power as a full Laravel application.
It might become your API server or push (or pull) from your queues for your main Laravel application to process. Laravel and Lumen are designed to make a perfect team, and, when used together, allow you to build powerful, micro-service driven applications. It might collect data from multiple places and then serve it out in a normalized manner. If it’s a single component, especially if it’s high traffic, it may be worth trying it out with Lumen.
With Lumen your API will support at least twelve hundred (1200) requests in a range of 10 seconds with 10 simultaneous requests. This great speed makes it the perfect candidate for implementing a RESTful API in it, and the best, it is completely easy!
Lumen is not as configurable as the Laravel framework. The performance improvement is achieved by taking away some of the flexibility of the framework in terms of configuration and amending the default boot process. For example, it is not possible to override any framework “bootstrappers” to drastically alter how the framework is constructed. Also, unlike Laravel, Lumen is not intended to be used with additional Laravel “packages” such as debug bars, CMS systems, etc.
What is more, Lumen does not use Symfony’s Routing component. Instead, \- is used for better performance. If you need Symfony Routing features such as sub-domain routing or optional parameters, you should consider using the full Laravel framework.
Lumen utilizes Composer to manage its dependencies. Composer is a tool for dependencies management which enables you to easily download all the required packages/libraries your PHP project can use. The tool is dependent on the PHP service and it is available on all our Shared Hosting packages.
First, download the Lumen installer using Composer:
composer global require "laravel/lumen-installer"
Choose the directory in your PATH so the lumen executable can be located by our system.
For instance, lumen new blog will create a directory named blog containing a fresh Lumen installation with all of Lumen’s dependencies already installed.
/home/<user>/.composer/vendor/laravel/lumen-installer/lumen new blog
Once installed, the lumen new command will create a fresh Lumen installation in the directory you specify.
To Lumen or not to Lumen?
Interested in giving Lumen a try? Millions of programmers of PHP use Laravel and all of them are agree with the incredible ease and agility that Lumen provides for micro services, including, of course, RESTful APIs, so, what are you waiting for? Take it now!
Plus if you start your project with Lumen and eventually need even more power, moving to the full featured Laravel is a simple process.
The latest tips and news from the industry straight to your inbox!
Join 30,000+ subscribers for exclusive access to our monthly newsletter with insider cloud, hosting and WordPress tips!