Get started with Laravel Lumen / Introduction

wix web Development Company
5 min readJan 7, 2021

The founder of Laravel, Taylor Otwell created Lumen micro framework in 2015. Lumen is a PHP-based, open-source microservice framework. It is useful for designing Laravel Lumen APIs and fast operating microservices, which render support to large and complex micro framework Laravel applications.

The key advantage that comes in with the use of Lumen is speed. It is classified as a micro-framework, which implies that while being fast; it is a minor and a leaner version when we take a full application web framework into perspective. Lumen is currently the fastest micro-framework available.

Lumen Version History

Lumen 5.0

The very first Lumen framework, created in 2015.

Lumen 5.0.4

A developer can upgrade to Lumen 5.0.4 by making minor changes to the bootstrap/app.php file.

Lumen 5.1.0

Lumen 5.1.0 utilizes Laravel 5.1 components. A few of the important features of the version are middleware parameters, testing improvements, and event broadcasting.

Lumen 5.2.0

Lumen 5.2.0 similarly utilizes Laravel 5.2 components. The version had some major changes to the framework. With the elimination of the concept of sessions & views, only stateless APIs are supported. Similarly, authentication needs to be done statelessly using headers or API tokens. Testing helpers no longer form a part of Lumen 5.2.

The latest lumen micro framework versions are Lumen 5.3.0 and Lumen 5.6.0.

Lumen Installation Process

System Requirements

  • Mbstring PHP Extension
  • PDO PHP Extension
  • OpenSSL PHP Extension
  • PHP >= 7.1.3

From Where You Can Download Lumen?

You can use this link to download Lumen.

Installing Lumen

Lumen uses the composer, which is PHP’s dependency manager. So, a composer should be installed over your machine before you install Lumen. A professional Laravel developer will be in the best position to see through the accomplishment of the task.

Via Lumen Installer

Download the installer using the composer by using the command

composer global require “laravel/lumen-install"

Place ~/.composer/vendor/bin in your PATH to let your system locate the lumen executable.

Now, lumen new will create a directory with the specified name. It will have a fresh installation of Lumen with all the dependencies taken care of.

Via Composer Create-Project

One can also install Lumen by running the Composure create-project command

composer create-project –prefer-dist laravel/lumen blog

Configuration In Lumen

Configuration options for Lumen are in an .env file.

How To Access Configuration Values?

Global config helper function may be used to access configuration values. The syntax that should be used for the same is:

$value = config(‘filename.option’)

One also has a choice at setting the default value which returns when the configuration option doesn’t exist.

For setting configuration values at runtime, one can pass an array to the config helper:

config(['app.timezone' => 'America/Chicago']);

Setting Up Application Key

Once one has Lumen installed over his machine, one should first set up his application key to a random string. This string is most often 32 characters long, and it may be in the format of an .env file.

In case the application key is not active, the data will not be secured. So, this is an important step to keep in mind when working with Lumen.

Key Features of Lumen

1. Routing

The routing facility, in the case of Lumen, is provided through Fast Rout. Fast Rout is a library for enabling a quick implementation of a regular expression based router. A developer should define all routes of the app in the routes/web.php file. Lumen routes simply accept a URI & a closure.

This is the list of router methods used for Lumen framework:

$app->get($uri, $callback);

$app->post($uri, $callback);

$app->put($uri, $callback);

$app->patch($uri, $callback);

$app->delete($uri, $callback);

$app->options($uri, $callback);

2. Authentication

Since Lumen does not support the concept of sessions, all requests authenticate through stateless mechanisms, like API tokes.

The AuthServiceProvider is located in the app/providers. It involves a single call to Auth::viaRequest. The method accepts a closure. When an authentication request arrives, the closure comes into play. In case no authentication is found for the request, the method returns a null.

The Auth:user() method helps obtain the authenticated user. One may alternately use $request->user() method on an IlluminateHttpRequest instance for the same.

3. Authorization

Lumen has a straightforward way to organize authorization logic and makes a simplified way available to access these resources. In Lumen, authorization is similar to how it is in the Laravel micro framework, barring a few adjustments.

The ways to define abilities are different in Laravel and Lumen. One can use the Gate facade in AuthServiceProvider to define abilities in Lumen:

Gate::define('update-post', function ($user, $post) {

return $user->id === $post->user_id;

});

Similarly in Lumen, one cannot define $policies array on the AuthServiceProvider. But one can call the policy on Gate facade

Gate::policy(Post::class, PostPolicy::class);

4. Caching

In Lumen caching, mechanisms have similarities with the Laravel framework. The code used by Lumen cache driver is the same as the code used by Laravel. Lumen also supports popular caching backends such as Redis and Memcached.

5. Encryption

Before one uses Lumen’s encrypter, one should go to the application key option of the .env file, and set it to a random, 32 character string. Authorization and authentication are some of the ways to uplift business using Laravel app development.

Values are encrypted in Lumen using the Crypt façade. All values are encrypted using the AES-256-CBC cipher and OpenSSL. The values are signed with a MAC (Message Authentication Code) for detecting any modification.

6. Errors & Logging

Error & exception handling mechanism is inherited in your project by default upon starting a new project in the Lumen framework. Additionally, Lumen is also integrated with Monolog. Monolog is a logging library that supports a range of log handlers. Managing errors explicitly is hence not needed.

7. Events

Lumen’s events make a simple observer implementation possible. Using the same, one can subscribe and listen to the events in his app. The listeners are stored in app/Listeners directory and events classes are stored in the app/event.

Similar to the Laravel framework, the EventServiceProvider is included in the Lumen app as well. It offers a way of registering event listeners.

Conclusion

Across the years gone by, agile web applications have been in high demand. Lumen makes a preferred option to go ahead with in such circumstances, in particular for lightweight and small apps. With our expertise over Lumen, Cyblance is the preferred top laravel development companies for your requirements.

--

--

wix web Development Company

eCommerce hemisphere is progressively competitive today, and a trending Wix website design brings success to your website at many levels.