如何访问父控制器中每个请求中的存储?

发布于 2024-08-15 19:45:40 字数 247 浏览 1 评论 0原文

我正在尝试重构我的 Catalyst 应用程序,我想为我的控制器引入一个通用基类。对于每个请求,该基本控制器都会加载一些数据并将其他一些数据放入存储中。我在获取储藏室时遇到了一些麻烦。简单的解决方案是在基本控制器中实现默认的 auto 操作。这是可行的,但我必须记住始终在派生控制器中调用 super auto 。这个很容易忘记,有更好的解决方案吗?换句话说:是否有一种简单的方法来利用请求处理,并且在派生控制器中不容易被破坏?

I am trying to refactor my Catalyst application and I would like to introduce a common base class for my controllers. This base controller would load some data and put some other data into the stash for every request. I have got some trouble getting to the stash. Simple solution would be to implement a default auto action in the base controller. This works, but I have to remember to always call the super auto in the derived controllers. This is easy to forget, is there a better solution? In other words: Is there a simple way to tap into the request processing that wouldn’t be so easy to break in the derived controllers?

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

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

发布评论

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

评论(1

无声静候 2024-08-22 19:45:40

我认为你不需要继承来实现你的目标。您可能有其他原因认为继承对于您的应用程序来说是一个好主意,但通常来说,当其他类组合方法更合适时,继承通常会被过度使用。

特别是对于这种情况,Catalyst 通过允许您在根控制器中指定 auto 方法来提供此功能,该方法将始终在您的 more 的 auto 方法之前调用。每个请求的特定控制器。无需继承。

I don't think you need inheritance in order to accomplish your goal. You might have other reasons why inheritance is a good idea for your application, but it seems as a rule that inheritance is generally overused when other methods of class composition would be more appropriate.

In particular for this case, Catalyst provides for this functionality by allowing you to specify an auto method in your Root controller, which will always be invoked before the auto methods of your more particular controllers for every request. No inheritance required.

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