DEV Community 👩‍💻👨‍💻

Cover image for Differences between Laravel and Lumen
Wahyu Kristianto
Wahyu Kristianto

Posted on

Differences between Laravel and Lumen

Many ask what is the difference between Laravel and Lumen. Here is my answer on StackOverflow:

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?

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);
Enter fullscreen mode Exit fullscreen mode

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',
    ],
];
Enter fullscreen mode Exit fullscreen mode
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);
Enter fullscreen mode Exit fullscreen mode

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;
});
Enter fullscreen mode Exit fullscreen mode
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);
Enter fullscreen mode Exit fullscreen mode
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);
}
Enter fullscreen mode Exit fullscreen mode

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);
}
Enter fullscreen mode Exit fullscreen mode

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);
Enter fullscreen mode Exit fullscreen mode

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);
Enter fullscreen mode Exit fullscreen mode

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);
Enter fullscreen mode Exit fullscreen mode

Testing

Features Laravel Lumen
Basic ns ns

Top comments (2)

Collapse
 
ziarv profile image
Zia

Laravel do not recommend using lumen now check this out lumen.laravel.com/docs/9.x#install...

Collapse
 
naubit profile image
Al - Naubit

Great article, keep the good work! Liked and followed! 🚀

🌚 Friends don't let friends browse without dark mode.

Sorry, it's true.