Mojolicious 自定义会话

发布于 2024-12-24 18:56:12 字数 1482 浏览 0 评论 0原文

我正在尝试使用 Mojolicious 的数据库会话,而不是使用签名 cookie 的内置会话。

startup 子例程中,我有类似的内容:

my $dbh = DBI->connect(                                                                                                                                  
                        $config->{database}->{dsn},                                                                                                      
                        $config->{database}->{user},                                                                                                     
                        $config->{database}->{password},
                      );

my $session = MojoX::Session->new(
    store     => [dbi => {dbh => $dbh}],  # use MojoX::Session::Store::Dbi
    transport => 'cookie',                # this is by default
    ip_match  => 1
);

(ref($self))->attr( 'session' => sub {                
                return $session;
                } );

我想使用会话对象,例如 $self->session$self->app-> ;控制器中的会话

不幸的是它不起作用 - 它使用以前的会话(来自不同的浏览器)。

这让我发疯 - 我今天所做的就是让这项工作成功 - 我已经阅读了所有可用的文档,还有 MojoX::Session 及其所有相关类,尝试过大约923847293847239847 方法让它工作,但似乎没有任何效果。

PS:我在数据库中创建了 session 表。

您知道我应该做什么才能使用 Mojolicious 的数据库会话吗?

I am trying to use database sessions with Mojolicious instead of the builtin ones that are working with signed cookies.

In the startup subroutine I have something like:

my $dbh = DBI->connect(                                                                                                                                  
                        $config->{database}->{dsn},                                                                                                      
                        $config->{database}->{user},                                                                                                     
                        $config->{database}->{password},
                      );

my $session = MojoX::Session->new(
    store     => [dbi => {dbh => $dbh}],  # use MojoX::Session::Store::Dbi
    transport => 'cookie',                # this is by default
    ip_match  => 1
);

(ref($self))->attr( 'session' => sub {                
                return $session;
                } );

And I want to use the session object like $self->session or $self->app->session in controllers.

Unfortunately it doesn't work - it uses previous sessions (from different browsers).

This drives me crazy - all I tried today was to make this work - I've read all the documentation available, also the source of MojoX::Session and all its related classes, tried in about 923847293847239847 ways to make it work, but nothing seems to do it.

PS: I created the session table in the db.

Do you know what I should do in order to be able to use DB sessions with Mojolicious?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

不念旧人 2024-12-31 18:56:12

您可以将 MojoX::Session 作为 startup 函数中的插件连接到应用程序。

use Mojolicious::Plugin::Session;

[...]

sub startup {
  my $self = shift;

  [...]

  $self->plugin( session => {
    stash_key => 'mojox-session',
    store     => [dbi => {dbh => $dbh}],  # use MojoX::Session::Store::Dbi
    transport => 'cookie',
    ip_match  => 1
  });

  [...]

之后,您将可以通过控制器中的隐藏密钥“mojox-session”访问会话。

例如:

$self->stash('mojox-session')->data('something');

You can connect MojoX::Session to the application as a plugin in a startup function.

use Mojolicious::Plugin::Session;

[...]

sub startup {
  my $self = shift;

  [...]

  $self->plugin( session => {
    stash_key => 'mojox-session',
    store     => [dbi => {dbh => $dbh}],  # use MojoX::Session::Store::Dbi
    transport => 'cookie',
    ip_match  => 1
  });

  [...]

Afterwards, you'll have access to session through stash key 'mojox-session' in controllers.

For example:

$self->stash('mojox-session')->data('something');
最好是你 2024-12-31 18:56:12

您可以使用您喜欢的任何类型的会话后端。只需创建您自己的从 Mojolicious::Controller 派生的控制器基类并覆盖 session(),如下所示:

package NiceController;
use Mojo::Base 'Mojolicious::Controller';
sub session { # custom code here }
1;

然后在startup() 中,将您的控制器类设置为默认值:

$self->controller_class('NiceController');

最后,确保您的应用程序控制器继承自 NiceController 而不是 Mojolicious: :Controller

让您重写的 session() 函数像内置函数一样工作可能是一个好主意,以避免将来的混乱。

-xyz

You can use whatever sort of session backend you like. Just create your own controller base class derived from Mojolicious::Controller and override session(), like so:

package NiceController;
use Mojo::Base 'Mojolicious::Controller';
sub session { # custom code here }
1;

then in startup(), set your controller class as the default:

$self->controller_class('NiceController');

Finally, make sure your application controllers inherit from NiceController instead of Mojolicious::Controller

It's probably a good idea to make your overridden session() function work just like the built-in one, to avoid future confusion.

-xyz

还如梦归 2024-12-31 18:56:12

$app->session 方法保留用于使用内置会话。

您应该看看 Mojolicious 助手,您可能想使用不同的方法名称以避免冲突。

The $app->session method is reserved for using the built-in sessions.

You should take a look at the Mojolicious helpers and you probably want to use a different method name to avoid conflict.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文