如何访问父控制器中每个请求中的存储?
我正在尝试重构我的 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为你不需要继承来实现你的目标。您可能有其他原因认为继承对于您的应用程序来说是一个好主意,但通常来说,当其他类组合方法更合适时,继承通常会被过度使用。
特别是对于这种情况,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 theauto
methods of your more particular controllers for every request. No inheritance required.