在 lumen 框架中启用 session
Enabling session in lumen framework
我有两个(但让我们想象更多)micro-services (API) 需要知道经过身份验证的用户。理想情况下,我只想恢复他们的会话。
所有 micro-services 都在为会话使用相同的存储空间:redis。
所有 API 调用都将具有 Cookie header,因此所有服务都将能够根据该 cookie 恢复会话。我已经通过 PHP $_SESSIONs.
成功实现了这个
现在的问题是:您将如何使用 Laravel/Lumen 来实现它?
2021 年 3 月 5 日最后更新
(这个答案得到了 Laravel 社区的大量关注,所以我想更新它。)
Laravel 已正式停止支持 laravel/lumen
框架中的会话和视图,从版本 5.2 开始。
但是 laravel
仍然有一个组件 illuminate/session
可以安装在 lumen/framework
中,我们可以尝试一下。
步骤 - 1
使用
安装illuminate/session
composer require illuminate/session
步骤 - 2
现在转到 bootstrap/app.php
并添加此中间件
$app->middleware([
\Illuminate\Session\Middleware\StartSession::class,
]);
添加上述中间件的目的是在每次请求时启动会话并在服务响应之前保存会话。
步骤 - 3
现在添加 config/session.php
,因为它默认不存在于 Lumen
中。您可以从 Laravel official repo.
中获取 session.php
步骤 - 4
通过
创建框架会话存储目录
mkdir -p storage/framework/sessions
感谢
步骤 - 5
在 bootstrap/app.php
中为 \Illuminate\Session\SessionManager
添加绑定
$app->singleton(Illuminate\Session\SessionManager::class, function () use ($app) {
return $app->loadComponent('session', Illuminate\Session\SessionServiceProvider::class, 'session');
});
$app->singleton('session.store', function () use ($app) {
return $app->loadComponent('session', Illuminate\Session\SessionServiceProvider::class, 'session.store');
});
感谢 @xxRockOnxx 找到 loadComponent
方法。
它需要 3 个参数,
- 第一个是
config
文件名。 (文件应该存在于 config/
目录中)
- 第二个是服务提供商 FQN
- 第三个是return这个方法。
loadComponent
只是在构建 ServiceProvider
时调用 $app->register
并注入 $app
使用方法
// Save Session
$router->get('/', function (\Illuminate\Http\Request $request) {
$request->session()->put('name', 'Lumen-Session');
return response()->json([
'session.name' => $request->session()->get('name')
]);
});
// Test session
$router->get('/session', function (\Illuminate\Http\Request $request) {
return response()->json([
'session.name' => $request->session()->get('name'),
]);
});
我还在 github 上添加了支持从 lumen 框架 v5.6 一直到当前版本 v8.0 的示例。
重要的是你也使用$request->session()
,否则将无法工作。
我尝试了上面提到的解决方案,但是,如果使用默认设置,还需要创建一个文件夹storage/framework/sessions
。
已接受的答案已过时。
我在回答这个
时回答并解释了如何正确地做到这一点
我还在 Laracasts
上发布了我的问题是什么
引用:
the solution that is found in the link you gave is that, first it tells you to manually register the SessionManager
to prevent the unresolvable depedency parameter #0 $app
then also register the existing SessionServiceProvider
which also binds another instance SessionManager
.
Problem with that is, some components use the other instance and other parts use the new one which causes my auth attempt
session not being save despite actually being put
inside.
我有两个(但让我们想象更多)micro-services (API) 需要知道经过身份验证的用户。理想情况下,我只想恢复他们的会话。
所有 micro-services 都在为会话使用相同的存储空间:redis。
所有 API 调用都将具有 Cookie header,因此所有服务都将能够根据该 cookie 恢复会话。我已经通过 PHP $_SESSIONs.
成功实现了这个现在的问题是:您将如何使用 Laravel/Lumen 来实现它?
2021 年 3 月 5 日最后更新
(这个答案得到了 Laravel 社区的大量关注,所以我想更新它。)
Laravel 已正式停止支持 laravel/lumen
框架中的会话和视图,从版本 5.2 开始。
但是 laravel
仍然有一个组件 illuminate/session
可以安装在 lumen/framework
中,我们可以尝试一下。
步骤 - 1
使用
安装illuminate/session
composer require illuminate/session
步骤 - 2
现在转到 bootstrap/app.php
并添加此中间件
$app->middleware([
\Illuminate\Session\Middleware\StartSession::class,
]);
添加上述中间件的目的是在每次请求时启动会话并在服务响应之前保存会话。
步骤 - 3
现在添加 config/session.php
,因为它默认不存在于 Lumen
中。您可以从 Laravel official repo.
session.php
步骤 - 4
通过
创建框架会话存储目录mkdir -p storage/framework/sessions
感谢
步骤 - 5
在 bootstrap/app.php
中为 \Illuminate\Session\SessionManager
$app->singleton(Illuminate\Session\SessionManager::class, function () use ($app) {
return $app->loadComponent('session', Illuminate\Session\SessionServiceProvider::class, 'session');
});
$app->singleton('session.store', function () use ($app) {
return $app->loadComponent('session', Illuminate\Session\SessionServiceProvider::class, 'session.store');
});
感谢 @xxRockOnxx 找到 loadComponent
方法。
它需要 3 个参数,
- 第一个是
config
文件名。 (文件应该存在于config/
目录中) - 第二个是服务提供商 FQN
- 第三个是return这个方法。
loadComponent
只是在构建 ServiceProvider
$app->register
并注入 $app
使用方法
// Save Session
$router->get('/', function (\Illuminate\Http\Request $request) {
$request->session()->put('name', 'Lumen-Session');
return response()->json([
'session.name' => $request->session()->get('name')
]);
});
// Test session
$router->get('/session', function (\Illuminate\Http\Request $request) {
return response()->json([
'session.name' => $request->session()->get('name'),
]);
});
我还在 github 上添加了支持从 lumen 框架 v5.6 一直到当前版本 v8.0 的示例。
重要的是你也使用$request->session()
,否则将无法工作。
我尝试了上面提到的解决方案,但是,如果使用默认设置,还需要创建一个文件夹storage/framework/sessions
。
已接受的答案已过时。
我在回答这个
我还在 Laracasts
上发布了我的问题是什么引用:
the solution that is found in the link you gave is that, first it tells you to manually register the
SessionManager
to prevent theunresolvable depedency parameter #0 $app
then also register the existingSessionServiceProvider
which also binds another instanceSessionManager
.Problem with that is, some components use the other instance and other parts use the new one which causes my auth
attempt
session not being save despite actually beingput
inside.