gpt4 book ai didi

zend-framework - Zend Framework - 我如何在 Controller 的一些 Action 之间共享通用代码?

转载 作者:行者123 更新时间:2023-12-04 06:37:14 24 4
gpt4 key购买 nike

为了让我的 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
}

那么我该如何重构它以将通用代码放在一个地方并能够访问 $columnsfirstAction()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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com