Differences and Similarities Between Lumen and Laravel

PhpLaravelLumen

Php Problem Overview


I read the documentation and it seems Lumen is Laravel with less features. I must be missing something. I am looking for a comparison table of the components and features of both Laravel and Lumen. Does anyone know the differences?

Php Solutions


Solution 1 - Php

Update (5.2)

With the latest version of Lumen (5.2) the Microframework focuses on Stateless APIs.
The documentation states: > Lumen 5.2 represents a shift on slimming Lumen to focus solely on serving stateless, JSON APIs. As such, sessions and views are no longer included with the framework. If you need access to these features, you should use the full Laravel framework.


Original answer (<= 5.1)

Lumen is all about speed. It's faster and can handle more requests per second than Laravel.

Laravel is a framework that glues together a lot of components (3rd party and Laravels own components). Now Lumen makes use of a lot of the same components, but strips down the bootstrapping process to a minimum. You could say it is a "different glue", the components (and therefore a lot of the features) are mostly the same.

The performance improvement is achieved by taking away some of the flexibility of the framework in terms of configuration and altering the default boot process.

Besides that, more features are disabled by default and have to be activated before they can be used. As an example: Facades (like DB::table())
You first need to uncomment this line in bootstrap/app.php to enable them:

// $app->withFacades();

The same goes for Dotenv environment files and Eloquent.

For routing Lumen uses nikic/FastRoute instead of symfonys router because it performs a lot better and gives the micro-framework another big boost.

Other than that pretty much everything is the same as in Laravel.

Good reads on Lumen

Solution 2 - Php

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 sacrified the flexibility of Laravel for speed.

However, you can add Laravel components to Lumen to extend it, so it can be used for more than just micro-services and API. However, if your goal is to extend Lumen to become a website, you might as well use Laravel instead.

They also have different use cases. Lumen and Laravel are meant to work together. For APIs and services are frequently get called, use Lumen. For user-facing applications, use Laravel.


This answer is taken from a blog post I wrote that explains the difference between Lumen and Laravel.

Solution 3 - Php

Quote from Matt Stauffer

> Lumen has the same foundation as Laravel, and many of the same components. But Lumen is built for microservices, not so much for user-facing applications (although it can be used for anything.) As such, frontend 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.

You can read more here

Solution 4 - Php

Lumen microframework is a lightweight version of Laravel full-stack framework. Lumen use the Laravel syntax and components, and can be 'upgrade' easily to Laravel.

Lumen is a more specialized (and stripped-down) framework designed for Microservices development and API development. So, some of the features in Laravel such as HTTP sessions, cookies, and templating are not needed and Lumen takes them away, keeping what's essential - routing, logging, caching, queues, validation, error handling and a couple of others.

Solution 5 - Php

Laravel 9.x and Lumen 9.x

Routing
Basic
Features Laravel Lumen
GET Route::get($uri, $callback) $router->get($uri, $callback)
POST Route::post($uri, $callback) $router->post($uri, $callback)
PUT Route::put($uri, $callback) $router->put($uri, $callback)
PATCH Route::patch($uri, $callback) $router->patch($uri, $callback)
DELETE Route::delete($uri, $callback) $router->delete($uri, $callback)
OPTION Route::option($uri, $callback) $router->option($uri, $callback)
Multiple HTTP verbs Route::match($types, $uri, $callback) ns
All HTTP verbs Route::any($uri, $callback) ns
Redirect Routes
Features Laravel Lumen
Basic Route::redirect($from, $to, $status); ns
Premanent Route::permanentRedirect($from, $to); ns
View Routes
Features Laravel Lumen
Basic Route::view($from, $to); ns
Route Parameters
Features Laravel Lumen
Parameters ns ns
Parameters & Dependency Injection ns ns
Required Parameters ns ns
Required Parameters ns ns
Regular Expression Constraints ns ns
Global Constraints ns ns
Named Routes
Features Laravel Lumen
Basic Route::get($uri, $callback)->name('profile') $router->get($uri, ['as' => 'profile', $callback])
Generating URLs To Named Routes route('profile') route('profile')
Inspecting The Current Route by Name $request->route()->named('profile') boolean ns
Route Groups
Features Laravel Lumen
Middleware Route::middleware($middleware) $router->group(['middleware' => $middleware], $callback)
Controllers Route::controller(ProfileController::class) ns
Subdomain Routing Route::domain('{account}.example.com') ns
Namespaces Route::namespace($namespace) $router->group(['namespace' => $namespace], $callback)
Route Prefixes Route::prefix('admin') $router->group(['prefix' => 'admin'], $callback)
Route Name Prefixes Route::name('admin.') ns
Route Model Binding
Features Laravel Lumen
Implicit Binding ns ns
Implicit Enum Binding ns ns
Explicit Binding ns ns
Fallback Routes
Features Laravel Lumen
Basic Route::fallback() ns
Route Caching
Features Laravel Lumen
Basic ns ns

Middleware
Features Laravel Lumen
Defining Middleware artisan make:middleware Manual
Global Middleware ns ns
Assigning Middleware To Routes ns ns
Middleware Groups ns ns
Middleware Parameters ns ns
Terminable Middleware ns ns

CSRF Protection
Features Laravel Lumen
Basic ns ns

CSRF removed since Lumen version 5.2


Controllers
Features Laravel Lumen
Defining Controller artisan make:controller Manual
Basic ns ns
Single Action Controllers ns ns
Controller Middleware ns ns
Resource Controllers ns ns
Dependency Injection & Controllers ns ns

Requests
Features Laravel Lumen
Accessing The Request ns ns
Request Path & Method ns ns
Request Headers ns ns
Request IP Address ns ns
Content Negotiation ns ns
PSR-7 Requests ns ns
Retrieving Input ns ns
Determining If Input Is Present ns ns
Merging Additional Input ns ns
Old Input ns ns
Cookies ns ns
Input Trimming & Normalization ns ns
Retrieving Uploaded Files ns ns
Moving Uploaded Files ns ns

Response
Features Laravel Lumen
Attaching Headers To Responses ns ns
Attaching Cookies To Responses ns ns
Redirects ns ns
View Responses ns ns
JSON Responses ns ns
File Downloads ns ns
File Responses ns ns

Views & Blade
Features Laravel Lumen
Basic ns ns
Blade ns ns

Session
Features Laravel Lumen
Basic ns ns

Session removed since Lumen version 5.2


Validation
Features Laravel Lumen
Basic ns ns
Form Requests ns ns
The $this->validate Method ns The $this->validate helper which is available in Lumen will always return a JSON response with the relevant error messages. This is in contrast to the Laravel version of the method which will return a redirect response if the request is not an AJAX request. Since Lumen is stateless and does not support sessions, flashing errors to the session is not a possibility. Unlike Laravel, Lumen provides access to the validate method from within Route closures.
The exists And unique Rules ns If you would like to use the exists or unique validation rules, you should uncomment the $app->withEloquent() method call in your bootstrap/app.php file.
The $errors View Variable ns Lumen does not support sessions out of the box, so the $errors view variable that is available in every view in Laravel is not available in Lumen. Should validation fail, the $this->validate helper will throw Illuminate\Validation\ValidationException with embedded JSON response that includes all relevant error messages.

Errors & Logging
Features Laravel Lumen
Error ns ns
Logging ns ns

Artisan Console
Features Laravel Lumen
Running Commands ns ns
Writing Commands ns ns

Cache
Features Laravel Lumen
Basic ns ns

Before using the Cache facade, be sure you have uncommented the $app->withFacades() method call in your bootstrap/app.php file.

Redis Support

Before using a Redis cache with Lumen, you will need to install the illuminate/redis package via Composer. Then, you should register the Illuminate\Redis\RedisServiceProvider in your bootstrap/app.php file:

$app->register(Illuminate\Redis\RedisServiceProvider::class);

If you have not called $app->withEloquent() in your bootstrap/app.php file, then you should call $app->configure('database'); in the bootstrap/app.php file to ensure the Redis database configuration is properly loaded.


Compiling Assets
Features Laravel Lumen
Mix ns ns

Events
Features Laravel Lumen
Basic ns ns
Generators

In Lumen, there are no generator commands to generate events and listeners for you, so you should simply copy the ExampleEvent or ExampleListener classes to define your own events and listeners. These example classes provide the basic structure of every event and listener.

Registering Events / Listeners

Like the full Laravel framework, the EventServiceProvider included with your Lumen application provides a convenient place to register all event listeners. The listen property contains an array of all events (keys) and their listeners (values). Of course, you may add as many events to this array as your application requires:

protected $listen = [
    'App\Events\ExampleEvent' => [
        'App\Listeners\ExampleListener',
    ],
];
Firing Events

You may use the event helper function or Event facade to fire events throughout your Lumen application. Again, these functions behave exactly like their full Laravel framework equivalent:

event(new ExampleEvent);
Event::dispatch(new ExampleEvent);

Authentication & Authorization
Features Laravel Lumen
Authentication ns ns
Authorization ns ns
Authentication

Authentication in Lumen, while using the same underlying libraries as Laravel, is configured quite differently from the full Laravel framework. Since Lumen does not support session state, incoming requests that you wish to authenticate must be authenticated via a stateless mechanism such as API tokens.

Authorization
Defining Abilities

The primary difference when using authorization in Lumen compared to Laravel is in regards to how abilities are defined. In Lumen, you may simply use the Gate facade in your AuthServiceProvider to define abilities:

Gate::define('update-post', function ($user, $post) {
    return $user->id === $post->user_id;
});
Defining Policies

Unlike Laravel, Lumen does not have a $policies array on its AuthServiceProvider. However, you may still call the policy method on the Gate facade from within the provider's boot method:

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

You may "check" abilities just as you would in the full Laravel framework. First, you may use the Gate facade. If you choose to use the facade, be sure to enable facades in your bootstrap/app.php file. Remember, we don't need to pass the User instance into the allows method since the currently authenticated user will automatically be passed to your authorization callback:

if (Gate::allows('update-post', $post)) {
    //
}

if (Gate::denies('update-post', $post)) {
    abort(403);
}

Of course, you may also check if a given User instance has a given ability:

if ($request->user()->can('update-post', $post)) {
    // The user is allowed to update the post...
}

if ($request->user()->cannot('update-post', $post)) {
    abort(403);
}

Database
Features Laravel Lumen
Basic Queries ns ns
Query Builder ns ns
Eloquent ORM ns ns
Migrations ns ns
Seeders ns ns

If you would like to use the DB facade, you should uncomment the $app->withFacades() call in your bootstrap/app.php file.


Email Verification & Resetting Passwords
Features Laravel Lumen
Email Verification ns ns
Resetting Passwords ns ns

Encryption & Hashing
Features Laravel Lumen
Encryption ns ns
Hashing ns ns

You should set the APP_KEY option of your .env file to a 32 character, random string. If this value is not properly set, all values encrypted by Lumen will be insecure.


Mail
Features Laravel Lumen
Basic ns ns

Queues
Features Laravel Lumen
Basic ns ns

Closure jobs are not supported by Lumen.

Generators

Lumen does not include generators for automatically creating new Job classes. Instead, you should copy the ExampleJob class that is included with the framework.

Dispatching Jobs

Again, you should consult the full Laravel queue documentation for complete information on dispatching queued jobs; however, just like in the Laravel framework, you may use the dispatch function to dispatch jobs from anywhere within your Lumen application:

dispatch(new ExampleJob);

Of course, you may also use the Queue facade. If you choose to use the facade, be sure to uncomment the call to $app->withFacades() in your bootstrap/app.php file:

Queue::push(new ExampleJob);

Service Container
Features Laravel Lumen
Basic ns ns
Accessing The Container

The Laravel\Lumen\Application instance is an extension of Illuminate\Container\Container, so it may be treated as the service container for your application.

Resolving Instances

To resolve things out of the container, you may either type-hint the dependency you need on a class that is already automatically resolved by the container, such as a route Closure, controller constructor, controller method, middleware, event listener, or queued job. Or, you may use the app function from anywhere in your application:

$instance = app(Something::class);

Testing
Features Laravel Lumen
Basic ns ns

Solution 6 - Php

Lumen it's a micro-framework that has the same foundation as Laravel, and many of the same components.

  • But Lumen is built for microservices, not so much for user-facing applications.
  • It's a massively stripped down version of Laravel, so it took away unneeded features for an API such as HTTP sessions and cookies, and also limited the number of configuration options.
  • Lumen makes use of a lot of the same components, but strips down the bootstrapping process to a minimum.
  • Its designed to be a lightweight API handling system so you want the code to boot up as quick as possible and to be able to return data back to the user as quick as possible.

To make it short

  • You have the same route management and the same MVC functionality, you have middleware functionality including throttling, but things like Eloquent are switched off by default although there is a setting to switch it on - its not like it has to be added via composer for example.
  • Also you don’t get things like tinker added in and the artisan command.

Some of the major differences

  • Laravel is a full-stack web application framework that packages or supports a lot of third-party tools and frameworks, whereas Lumen is a micro-framework that is used to develop microservices and API development with the intent of providing speed and high response time.
  • Laravel requires different kinds of configuration for the server, and other tools to be used along with the application, whereas Lumen micro framework is a light form of Laravel version that provides specialized features such as API development, cookies, templating, caching, logging, routing, HTTP sessions etc.
  • Laravel can be integrated with a different more number of tools than Lumen, whereas Lumen has fewer integration facilities with other tools.
  • Laravel performance will be good in the case of SQL queries and tuning the database from the application level, whereas Lumen’s performance declines in the case of SQL queries and fewer features compared to Laravel.
  • In Lumen if you need Eloquent you have to enable it.
  • In Lumen you don't have Blade template engine.
  • Laravel is good for building RESTful APIs (Application Programming Interfaces), whereas Lumen is one of the highest performing micro-framework API for building microservices.
  • Authentication in Lumen, while using the same underlying libraries as Laravel, is configured quite differently from the full Laravel framework. Since Lumen does not support session state, incoming requests that you wish to authenticate must be authenticated via a stateless mechanism such as API tokens.
  • Lumen has no inbuilt event queuing feature as Laravel has.

Solution 7 - Php

Why Lumen ?

> Lumen is the perfect solution for building Laravel based micro-services and blazing fast APIs. In fact, it's one of the fastest micro-frameworks available. It has never been easier to write stunningly fast services to support your Laravel applications. Doc

Lumen is a framework to build APIs, which will essentially serve JSON responses for your requests. That's it.

These kinds of applications are known as web services in general.

As of Lumen 5.2 update, it doesn't support laravel views, sessions etc... for that you will have to upgrade to full laravel framework.

Some components of the Laravel framework is swapped with other packages in favour of performance. check them here

Example scenarios where we can consider Lumen

  • You might want to open up some features of an application to other developers via API
  • Your application needs to support both web and mobile application, then it will be perfect to store the data in a DB wrapped in Lumen API.
  • When you consider scalability as an important point, you may need lumen

Solution 8 - Php

The main difference between laravel and lumen is, Laravel can have artisan commands, which lumen doesn't have.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionRanjeet KarkiView Question on Stackoverflow
Solution 1 - PhplukasgeiterView Answer on Stackoverflow
Solution 2 - Phpd4nyllView Answer on Stackoverflow
Solution 3 - PhpIvan DokovView Answer on Stackoverflow
Solution 4 - PhppardeepView Answer on Stackoverflow
Solution 5 - PhpWahyu KristiantoView Answer on Stackoverflow
Solution 6 - PhpDavide CasiraghiView Answer on Stackoverflow
Solution 7 - PhpShobiView Answer on Stackoverflow
Solution 8 - Phpvaibhavmht225View Answer on Stackoverflow