路由
基本
| 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) |
|
重定向路由
| Features |
Laravel |
Lumen |
| Basic |
Route::redirect($from, $to, $status); |
|
| Premanent |
Route::permanentRedirect($from, $to); |
|
查看路线
| Features |
Laravel |
Lumen |
| Basic |
Route::view($from, $to); |
|
路由参数
| Features |
Laravel |
Lumen |
| Parameters |
|
|
| Parameters & Dependency Injection |
|
|
| Required Parameters |
|
|
| Required Parameters |
|
|
| Regular Expression Constraints |
|
|
| Global Constraints |
|
|
命名路线
| 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 |
|
路由组
| 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.') |
|
路由模型绑定
| Features |
Laravel |
Lumen |
| Implicit Binding |
|
|
| Implicit Enum Binding |
|
|
| Explicit Binding |
|
|
后备路线
| Features |
Laravel |
Lumen |
| Basic |
Route::fallback() |
|
路由缓存
| Features |
Laravel |
Lumen |
| Basic |
|
|
中间件
| Features |
Laravel |
Lumen |
| Defining Middleware |
artisan make:middleware |
Manual |
| Global Middleware |
|
|
| Assigning Middleware To Routes |
|
|
| Middleware Groups |
|
|
| Middleware Parameters |
|
|
| Terminable Middleware |
|
|
CSRF 保护
| Features |
Laravel |
Lumen |
| Basic |
|
|
自 Lumen 版本 5.2987654545@
后删除了 CSRF
控制器
| Features |
Laravel |
Lumen |
| Defining Controller |
artisan make:controller |
Manual |
| Basic |
|
|
| Single Action Controllers |
|
|
| Controller Middleware |
|
|
| Resource Controllers |
|
|
| Dependency Injection & Controllers |
|
|
请求
| 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 |
|
|
响应
| Features |
Laravel |
Lumen |
| Attaching Headers To Responses |
|
|
| Attaching Cookies To Responses |
|
|
| Redirects |
|
|
| View Responses |
|
|
| JSON Responses |
|
|
| File Downloads |
|
|
| File Responses |
|
|
视图和刀片
| Features |
Laravel |
Lumen |
| Basic |
|
|
| Blade |
|
|
会话
| Features |
Laravel |
Lumen |
| Basic |
|
|
自 Lumen 版本后删除会话5.2
验证
| 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. |
错误和日志记录
| Features |
Laravel |
Lumen |
| Error |
|
|
| Logging |
|
|
工匠控制台
| Features |
Laravel |
Lumen |
| Running Commands |
|
|
| Writing Commands |
|
|
缓存
| Features |
Laravel |
Lumen |
| Basic |
|
|
在使用 Cache 外观之前,请确保您已取消注释您的 bootstrap/app.php 文件中的 $app->withFacades() 方法调用。
Redis 支持
在使用带有 Lumen 的 Redis 缓存之前,您需要通过 Composer 安装 illuminate/redis 包。然后,您应该在您的bootstrap/app.php 文件中注册Illuminate\Redis\RedisServiceProvider:
$app->register(Illuminate\Redis\RedisServiceProvider::class);
如果您尚未在bootstrap/app.php 文件中调用$app->withEloquent(),则应在bootstrap/app.php 文件中调用$app->configure('database'); 以确保正确加载Redis 数据库配置。
编译资产
| Features |
Laravel |
Lumen |
| Mix |
|
|
活动
| Features |
Laravel |
Lumen |
| Basic |
|
|
发电机
在 Lumen 中,没有生成器命令可以为您生成事件和侦听器,因此您应该简单地复制 ExampleEvent 或 ExampleListener 类来定义您自己的事件和侦听器。这些示例类提供了每个事件和侦听器的基本结构。
注册事件/监听器
与完整的 Laravel 框架一样,包含在 Lumen 应用程序中的 EventServiceProvider 提供了一个方便的位置来注册所有事件侦听器。 listen 属性包含所有事件(键)及其侦听器(值)的数组。当然,您可以根据应用程序的需要向该数组中添加任意数量的事件:
protected $listen = [
'App\Events\ExampleEvent' => [
'App\Listeners\ExampleListener',
],
];
触发事件
您可以使用 event 辅助函数或 Event 外观在整个 Lumen 应用程序中触发事件。同样,这些函数的行为与其完整的 Laravel 框架等效:
event(new ExampleEvent);
Event::dispatch(new ExampleEvent);
认证与授权
| Features |
Laravel |
Lumen |
| Authentication |
|
|
| Authorization |
|
|
认证
Lumen 中的身份验证虽然使用与 Laravel 相同的底层库,但其配置与完整的 Laravel 框架完全不同。由于 Lumen 不支持会话状态,因此您希望验证的传入请求必须通过 API 令牌等无状态机制进行验证。
授权
定义能力
与 Laravel 相比,在 Lumen 中使用授权的主要区别在于如何定义能力。在 Lumen 中,您可以简单地使用 AuthServiceProvider 中的 Gate 外观来定义能力:
Gate::define('update-post', function ($user, $post) {
return $user->id === $post->user_id;
});
定义策略
与 Laravel 不同,Lumen 在其 AuthServiceProvider 上没有 $policies 数组。但是,您仍然可以从提供者的 boot 方法中调用 Gate 外观上的 policy 方法:
Gate::policy(Post::class, PostPolicy::class);
检查能力
你可以像在完整的 Laravel 框架中一样“检查”能力。首先,您可以使用Gate 门面。如果您选择使用外观,请确保在您的bootstrap/app.php 文件中启用外观。请记住,我们不需要将User 实例传递给allows 方法,因为当前经过身份验证的用户将自动传递给您的授权回调:
if (Gate::allows('update-post', $post)) {
//
}
if (Gate::denies('update-post', $post)) {
abort(403);
}
当然,您也可以检查给定的User 实例是否具有给定的能力:
if ($request->user()->can('update-post', $post)) {
// The user is allowed to update the post...
}
if ($request->user()->cannot('update-post', $post)) {
abort(403);
}
数据库
| Features |
Laravel |
Lumen |
| Basic Queries |
|
|
| Query Builder |
|
|
| Eloquent ORM |
|
|
| Migrations |
|
|
| Seeders |
|
|
如果你想使用 DB 外观,你应该在你的 bootstrap/app.php 文件中取消注释 $app->withFacades() 调用。
电子邮件验证和重置密码
| Features |
Laravel |
Lumen |
| Email Verification |
|
|
| Resetting Passwords |
|
|
加密和散列
| Features |
Laravel |
Lumen |
| Encryption |
|
|
| Hashing |
|
|
您应该将 .env 文件的 APP_KEY 选项设置为 32 个字符的随机字符串。如果这个值没有正确设置,所有被 Lumen 加密的值都是不安全的。
邮件
| Features |
Laravel |
Lumen |
| Basic |
|
|
队列
| Features |
Laravel |
Lumen |
| Basic |
|
|
Lumen 不支持关闭作业。
发电机
Lumen 不包含用于自动创建新作业类的生成器。相反,您应该复制框架中包含的 ExampleJob 类。
派遣工作
同样,您应该查阅完整的 Laravel 队列文档以获取有关调度队列作业的完整信息;但是,就像在 Laravel 框架中一样,您可以使用 dispatch 函数从 Lumen 应用程序中的任何位置调度作业:
dispatch(new ExampleJob);
当然,您也可以使用Queue 门面。如果您选择使用外观,请务必在您的 bootstrap/app.php 文件中取消注释对 $app->withFacades() 的调用:
Queue::push(new ExampleJob);
服务容器
| Features |
Laravel |
Lumen |
| Basic |
|
|
访问容器
Laravel\Lumen\Application 实例是Illuminate\Container\Container 的扩展,因此它可以被视为您的应用程序的服务容器。
解析实例
要解决容器外的问题,您可以在已由容器自动解析的类上键入所需的依赖项,例如路由闭包、控制器构造函数、控制器方法、中间件、事件侦听器、或排队的工作。或者,您可以在应用程序的任何位置使用 app 函数:
$instance = app(Something::class);
测试
| Features |
Laravel |
Lumen |
| Basic |
|
|