Mojolicious 自定义会话
我正在尝试使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以将
MojoX::Session
作为startup
函数中的插件连接到应用程序。之后,您将可以通过控制器中的隐藏密钥“
mojox-session
”访问会话。例如:
You can connect
MojoX::Session
to the application as a plugin in astartup
function.Afterwards, you'll have access to session through stash key '
mojox-session
' in controllers.For example:
您可以使用您喜欢的任何类型的会话后端。只需创建您自己的从 Mojolicious::Controller 派生的控制器基类并覆盖 session(),如下所示:
然后在startup() 中,将您的控制器类设置为默认值:
最后,确保您的应用程序控制器继承自 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:
then in startup(), set your controller class as the default:
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
$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.