laravel 在流明框架中启用会话
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/47050984/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Enabling session in lumen framework
提问by rock3t
I have two (but let's image more) micro-services (API) which need to be aware of authenticated user. Ideally I would simple like to resume their sessions.
我有两个(但让我们想象更多)需要了解经过身份验证的用户的微服务 (API)。理想情况下,我只想恢复他们的会议。
All micro-services are using same storage for sessions: redis.
所有微服务都使用相同的会话存储:redis。
All API calls will have Cookie header, so all services will be able to resume sessions based on that cookie. I have successfully implemented this via PHP $_SESSIONs.
所有 API 调用都会有 Cookie 标头,因此所有服务都能够基于该 cookie 恢复会话。我已经通过 PHP $_SESSIONs 成功地实现了这一点。
Now the question: how would you go about implementing this with Laravel/Lumen?
现在的问题是:您将如何使用 Laravel/Lumen 实现这一点?
采纳答案by doesnotmatter
The accepted answer is outdated.
接受的答案已过时。
I answered and explained a bit how to properly do it in my answer on this question
我在我对这个问题的回答中回答并解释了一些如何正确地做到这一点
I also posted what is the problem on my question at Laracasts
我还在Laracasts上发布了我的问题有什么问题
To quote:
报价:
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.
在您提供的链接中找到的解决方案是,首先它告诉您手动注册
SessionManager
以防止unresolvable depedency parameter #0 $app
然后还注册现有的SessionServiceProvider
也绑定另一个实例SessionManager
。问题是,一些组件使用另一个实例,而其他部分使用新的实例,这导致我的身份验证
attempt
会话尽管实际上在put
里面,但没有被保存。
回答by rummykhan
Update as of 18th July 2019
截至 2019 年 7 月 18 日的更新
(This answer was getting a lot of attention from Laravel community so I thought of updating it.)
(这个答案得到了 Laravel 社区的很多关注,所以我想更新它。)
Laravel has officially stopped supporting sessions & views in laravel/lumen
framework from version 5.2 and on wards.
Laravellaravel/lumen
从 5.2 版和病房开始正式停止支持框架中的会话和视图。
But laravel
still have a component illuminate/session
which can be installed in lumen/framework
and we can play around with this.
但是laravel
仍然有一个illuminate/session
可以安装的组件,lumen/framework
我们可以玩这个。
Step - 1
第1步
install illuminate/session
using
安装illuminate/session
使用
composer require illuminate/session
composer require illuminate/session
Step - 2
第2步
Now goto bootstrap/app.php
and add this middleware
现在转到bootstrap/app.php
并添加这个中间件
$app->middleware([
\Illuminate\Session\Middleware\StartSession::class,
]);
Purpose of adding the above middleware is to start session on every request and save session before serving response.
添加上述中间件的目的是在每个请求上启动会话并在服务响应之前保存会话。
Step - 3
步骤 - 3
Now add config/session.php
, since it is not present in Lumen
by default. You can take session.php
from Laravel official repo.
现在添加config/session.php
,因为Lumen
默认情况下它不存在。您可以session.php
从Laravel 官方回购中获取。
Step - 4
第四步
Create framework session storage directory by
通过创建框架会话存储目录
mkdir -p storage/framework/sessions
Thanks to DayDream
感谢白日梦
Step - 5
步骤 - 5
In bootstrap/app.php
add bindings for \Illuminate\Session\SessionManager
在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');
});
Thanks to @xxRockOnxxfor finding loadComponent
method.
It takes 3 arguments,
感谢@xxRockOnxx寻找loadComponent
方法。它需要 3 个参数,
- first one is
config
file name. (file should be present inconfig/
directory) - second is ServiceProvider FQN
- third is return of this method.
- 第一个是
config
文件名。(文件应该存在于config/
目录中) - 第二个是 ServiceProvider FQN
- 第三是这个方法的返回。
loadComponent
just calls the $app->register
and inject $app
while building the ServiceProvider
loadComponent
只需在构建时调用$app->register
和注入$app
ServiceProvider
How to Use
如何使用
// 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'),
]);
});
I've also added example over github.
我还在 github 上添加了示例。
回答by Kevin Upton
It is important to that you also use $request->session()
, otherwise it will not work.
重要的是您还使用$request->session()
,否则它将无法正常工作。
回答by Daydream
I tried the solution mentioned above, however, it's also required to create a folder storage/framework/sessions
if using the default settings.
我尝试了上面提到的解决方案,但是,storage/framework/sessions
如果使用默认设置,还需要创建一个文件夹。