ChatGPT解决这个技术问题 Extra ChatGPT

Differences and Similarities Between Lumen and Laravel

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?

That doesn't seem right. Laravel is a full-featured framework where as Lumen is touted as a micro-framework derived from Laravel by sacrificing some of it's configurability and extensibility.
Lumen is pretty much Laravel with less features. That's the point.

C
Community

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

Documentation (especially the introduction)

Article from Matt Stauffer

Interview with Taylor Otwell (especially the last question)

Reddit discussion

Laracast: Introducing Lumen


Would it be that speedy if we'll start uncommenting stuff to get features back?
@SamDark Obviously it will get a bit slower with every feature you enable. But it will still be pretty fast since the disabled features are just one piece of the performance optimizations
So benchmark doesn't reflect overhead in real apps and in real apps it will be higher. OK.
@SamDark This blog post explains how it was benchmarked. As you can see the test application just returns "Hello World" without using models or other framework features, so the performance in a real application will be slower (although not noticeably in most cases. your application will still be ridiculously fast)
@lukasgeiter so as I understand it if I'm building an Ionic app with a RESTful Laravel API it would make more sense to use Lumen?
d
d4nyll

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.


As far as I know, cookies, sessions etc are still in there. However they will be lazy loaded and only really affect performance when used.
Lumen is my preferred framework now. I don't use Eloquent (or any ORMs), so Lumen fits me really well.
Let's say that I am building a web application and decide to write an API to further separate logic (and to allow easily third party applications). Would it be good practice to write the API with Lumen and write the website with Laravel?
It is a replacement. It's Laravel with a different router. Virually nothing was stripped out - just made optional. There's no legitimate reason to use Laravel when a supposedly faster Laravel (Lumen) is available.
I
Ivan Dokov

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


In addition, Lumen does not use Symfony's Routing component. Instead, nikic/fast-route is used for greater performance.
Matt Stauffer's bower setup for Laravel defeats the purpose of the default Bootstrap UI stuff in Laravel anyways, and as for Elixir, that's an npm install so it's not completely ready "out-of-the-box" I'd say. So we don't lose much in regards to these, by switching to Lumen. In Lumen, I just create a package.json and npm install Elixer, and setup bower and done...
However, after using Lumen, I can tell you there are frustrations. So far I've discovered that many of the request/routing/helpers are incomplete compared to what's available from Laravel. For example, you can't do a redirect()->intended. I'm hoping Lumen will catch up in these areas. I really like a Laravel without ORM (Eloquent) and so Lumen is very appealing to me.
@prograhammer, I don't think that that particular feature will ever be included in Lumen. I think that that would require a session which Lumen intentionally does away with. Additionally, I don't see much use for redirect()->intended() with an API, which is what Lumen focuses on.
Lumen is for APIS not for frontends
p
pardeep

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.


W
Wahyu Kristianto

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) All HTTP verbs Route::any($uri, $callback)

Redirect Routes

Features Laravel Lumen Basic Route::redirect($from, $to, $status); Premanent Route::permanentRedirect($from, $to);

View Routes

Features Laravel Lumen Basic Route::view($from, $to);

Route Parameters

Features Laravel Lumen Parameters Parameters & Dependency Injection Required Parameters Required Parameters Regular Expression Constraints Global Constraints

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

Route Groups

Features Laravel Lumen Middleware Route::middleware($middleware) $router->group(['middleware' => $middleware], $callback) Controllers Route::controller(ProfileController::class) Subdomain Routing Route::domain('{account}.example.com') 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.')

Route Model Binding

Features Laravel Lumen Implicit Binding Implicit Enum Binding Explicit Binding

Fallback Routes

Features Laravel Lumen Basic Route::fallback()

Route Caching

Features Laravel Lumen Basic

Middleware

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

CSRF Protection

Features Laravel Lumen Basic

CSRF removed since Lumen version 5.2

Controllers

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

Requests

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

Response

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

Views & Blade

Features Laravel Lumen Basic Blade

Session

Features Laravel Lumen Basic

Session removed since Lumen version 5.2

Validation

Features Laravel Lumen Basic Form Requests The $this->validate Method 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 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 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 Logging

Artisan Console

Features Laravel Lumen Running Commands Writing Commands

Cache

Features Laravel Lumen Basic

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

Events

Features Laravel Lumen Basic

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 Authorization

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 Query Builder Eloquent ORM Migrations Seeders

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 Resetting Passwords

Encryption & Hashing

Features Laravel Lumen Encryption Hashing

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

Queues

Features Laravel Lumen Basic

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

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


D
Davide Casiraghi

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.


S
Shobi

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


v
vaibhavmht225

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


This is not true, or at least not up to date. I don't know much about older versions, but I installed lumen 7.x and I have artisan commands