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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 16:53:37  来源:igfitidea点击:

Enabling session in lumen framework

laravelmicroserviceslumen

提问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 SessionManagerto prevent the unresolvable depedency parameter #0 $appthen also register the existing SessionServiceProviderwhich 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 attemptsession not being save despite actually being putinside.

在您提供的链接中找到的解决方案是,首先它告诉您手动注册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/lumenframework from version 5.2 and on wards.

Laravellaravel/lumen从 5.2 版和病房开始正式停止支持框架中的会话和视图。

But laravelstill have a component illuminate/sessionwhich can be installed in lumen/frameworkand we can play around with this.

但是laravel仍然有一个illuminate/session可以安装的组件,lumen/framework我们可以玩这个。

Step - 1

第1步

install illuminate/sessionusing

安装illuminate/session使用

composer require illuminate/session

composer require illuminate/session

Step - 2

第2步

Now goto bootstrap/app.phpand 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 Lumenby default. You can take session.phpfrom Laravel official repo.

现在添加config/session.php,因为Lumen默认情况下它不存在。您可以session.phpLaravel 官方回购中获取

Step - 4

第四步

Create framework session storage directory by

通过创建框架会话存储目录

mkdir -p storage/framework/sessions

Thanks to DayDream

感谢白日梦

Step - 5

步骤 - 5

In bootstrap/app.phpadd 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 loadComponentmethod. It takes 3 arguments,

感谢@xxRockOnxx寻找loadComponent方法。它需要 3 个参数,

  • first one is configfile name. (file should be present in config/directory)
  • second is ServiceProvider FQN
  • third is return of this method.
  • 第一个是config文件名。(文件应该存在于config/目录中)
  • 第二个是 ServiceProvider FQN
  • 第三是这个方法的返回。

loadComponentjust calls the $app->registerand inject $appwhile building the ServiceProvider

loadComponent只需在构建时调用$app->register和注入$appServiceProvider

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 上添加了示例。

https://github.com/rummykhan/lumen-session-example

https://github.com/rummykhan/lumen-session-example

回答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/sessionsif using the default settings.

我尝试了上面提到的解决方案,但是,storage/framework/sessions如果使用默认设置,还需要创建一个文件夹。