如何在 Catalyst 中请求结束时进行清理?

发布于 2024-07-11 16:24:06 字数 392 浏览 9 评论 0原文

我试图使用 Catalyst 在每个请求完成后调用一些代码。 基本上,我想运行一些代码作为 finalize 的一部分。 据说 Catalyst::Plugin::Observe 会执行此操作,但它似乎完全损坏(仅加载插件就会损坏 Catalyst)。

我正在尝试修复 Observe 插件,但事实证明这很顽固。

那么,是否有更好的方法来在每个请求结束时调用一些清理代码?

(注意:这是在模型中,而不是在控制器中,所以我不能只使用 sub end { ... }

I'm trying to get some code called after each request completes using Catalyst. Basically, I want to run some code as part of finalize. Supposedly Catalyst::Plugin::Observe will do this, but it appears completely broken (just loading the plugin breaks Catalyst).

I'm trying to fix the Observe plugin, but that's proving stubborn.

So, is there a better way to do get some cleanup code called at the end of each request?

(Note: This is in a model, not a controller, so I can't just use sub end { ... })

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

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

发布评论

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

评论(1

那一片橙海, 2024-07-18 16:24:06

实际上,您可以直接将代码添加到“MyApp”类中:

package MyApp;
use Catalyst ...;

...

sub finalize {
    my $c = shift;
    $c->NEXT::finalize(@_);
    # do your thing
}

这就是所有插件的工作方式; 它们只是成为您应用程序一部分的方法。

我确实同意让“finalize”生成一个事件来观察更干净......但这就是我们现在必须处理的:) 加入 irc.perl.org 上的 #catalyst,我们可以进一步讨论。 (你可能猜到了,我是jrockway。)

编辑回复:

(注意:这是在模型中,而不是在控制器中,所以我不能只使用 sub end { ... })

您确实知道 end 中有 $c代码>,对吗?

package Your::Model;

sub cleanup {
   my $self = shift;
   ...
}

package Your::Controller;

sub end :Private {
    my ($self, $c) = @_;
    $c->model('Your::Model')->cleanup( ... )
}

或者您可以从 MyApp::finalize 执行此操作,正如我上面建议的那样。

真正的问题是,为什么您的模型需要了解请求周期? 这听起来耦合非常紧密。

You can actually just add the code directly to your "MyApp" class:

package MyApp;
use Catalyst ...;

...

sub finalize {
    my $c = shift;
    $c->NEXT::finalize(@_);
    # do your thing
}

This is how all plugins work; they are just methods that become part of your app.

I do agree that making "finalize" generate an event to observe is cleaner... but this is what we have to work with for now :) Join #catalyst on irc.perl.org, and we can discuss further. (I am jrockway, as you may guess.)

Edited to reply to:

(Note: This is in a model, not a controller, so I can't just use sub end { ... })

You do know that you have $c in end, right?

package Your::Model;

sub cleanup {
   my $self = shift;
   ...
}

package Your::Controller;

sub end :Private {
    my ($self, $c) = @_;
    $c->model('Your::Model')->cleanup( ... )
}

Or you can do it from MyApp::finalize, as I suggested above.

The real question is, why does your model need to know about the request cycle? That sounds like awfully tight coupling.

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