如何在 lumen 5.2 上使用 Illuminate\Session\Middleware\StartSession::class

How can I use Illuminate\Session\Middleware\StartSession::class on lumen 5.2

我得到这个错误:

BindingResolutionException in Container.php line 839:
Unresolvable dependency resolving 
[Parameter #0 [ <required> $app ]] in class Illuminate\Support\Manager

bootstrap/app.php :

$app->middleware([
 Illuminate\Session\Middleware\StartSession::class,
]);

在添加 StartSession 中间件之前,将此依赖项注入容器:

$app->bind(Illuminate\Session\SessionManager::class, function ($app) {
    return new Illuminate\Session\SessionManager($app);
});

$app->middleware([
    Illuminate\Session\Middleware\StartSession::class,
]);

下面是在 Lumen 中激活会话需要做的事情的回顾(在 Lument 5.4 上测试):

config/session.php

从 Laravel 存储库下载会话配置。

bootstrap/app.php

// Load session config (otherwise it won't be loaded)
$app->configure('session');

// Add `Session` middleware
$app->middleware(Illuminate\Session\Middleware\StartSession::class);

// Add `SessionServiceProvider`
$app->register(Illuminate\Session\SessionServiceProvider::class);

// fix `BindingResolutionException` problem
$app->bind(Illuminate\Session\SessionManager::class, function ($app) {    
    return $app->make('session');
});

之后,您可以在控制器中使用 app('session') 访问会话。