为了让我的 Controller 尽可能干燥,我需要在我的 Controller 的 2 个 Action 之间共享一些公共(public)代码(一大块代码),而不是全部,我需要在我的 Action 中访问这个共享代码中的变量。
例如:
class FirstController extends Zend_Controller_Action {
public function firstAction() {
//common code here: contains an array $columns
}
public function secondAction() {
//common code here: contains an array $columns also
}
//other actions
}
那么我该如何重构它以将通用代码放在一个地方并能够访问
$columns
在 firstAction()
和 secondAction()
.谢谢。
最佳答案
我不建议您使用 基本 Controller .对于这么小的任务来说,它是过度杀伤和繁重的。由于您想在一个 Controller 中共享公共(public)代码,请改用 Action 助手和类属性 $columns
您可以将其作为参数发送给您的操作助手。
阅读更多关于 action helpers这里。
Action Helpers allow developers to inject runtime and/or on-demand functionality into any Action Controllers that extend Zend_Controller_Action. Action Helpers aim to minimize the necessity to extend the abstract Action Controller in order to inject common Action Controller functionality.
关于zend-framework - Zend Framework - 我如何在 Controller 的一些 Action 之间共享通用代码?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10246674/