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

标签 zend-framework zend-controller

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

相关文章:

zend-framework - 将 baseurl 值放入我的 Controller

mysql - ZEND 固件 : Joining two tables from different databases

php - Zend Lucene 的索引文件在哪里?

php - ZendFramework - 如何知道执行了哪个 Controller 和哪个特定方法?

php - Zf3 Controller 无法访问位于另一个模块中的模型类表

zend-framework - 如何在 Bootstrap 中访问 URL 参数

zend-framework - ZF2 - 已弃用 : ServiceManagerAwareInterface is deprecated and will be removed in version 3. 0,以及 ServiceManagerAwareInitializer

php - 如何在 Symfony2 项目中使用 Zend GData

php - Zend Framework 中的自定义过滤器/验证器

zend-framework - Zend Controller 操作 : _redirect() vs getHelper ('Redirector' )->gotoUrl()