简体   繁体   English

为 Zend 模块化结构创建通用控制器类

[英]Creating a common controller class for zend modular structure

I am using the zend modular structure for a project.我正在为一个项目使用 zend 模块化结构。 I have below directory structure我有以下目录结构

 application/
   modules/
    default/
        controllers/
            IndexController.php
            FooController.php
        models/
        views/
            scripts/
                index/
                foo/
            helpers/
            filters/
    blog/
        controllers/
            IndexController.php
        models/
        views/
            scripts/
                index/
            helpers/
            filters/
    news/
        controllers/
            IndexController.php
            ListController.php
        models/
        views/
            scripts/
                index/
                list/
            helpers/
            filters/

I want to create a common controller class to which I would extend in all module's controller我想创建一个通用控制器类,我将在所有模块的控制器中扩展到该类

eg I wan t to create a class like below例如,我不想创建一个如下所示的类

 class My_Common extends Zend_Controller_Action 
 {

  public function init()
  {
    
  }
}

In all module's controller I wan tto extend it like在所有模块的控制器中,我想像这样扩展它

 class News_IndexController extends My_Common
 {
     public function init()
 {
    
 }

 }

How could I achieve this?我怎么能做到这一点? I tried to create it in Default controller but its not working.我试图在默认控制器中创建它,但它不起作用。

You can easily achieve this by adding a namespace of my to the library folder.您可以通过将 my 的命名空间添加到库文件夹中轻松实现此目的。 And if you work with the zend library in mind the folder structure would look like this:如果您使用 zend 库,文件夹结构将如下所示:

the_common_folder_structure/ 
.../
library/
    zend/
    myNamespace/
      Controller/
        Common.php

Then in your modules just extend the MyNamespace_Controller_Common.然后在你的模块中扩展 MyNamespace_Controller_Common。

The way I would do it is add a "My" folder within the library directory and then replicate Zends class structure, eg :我这样做的方法是在库目录中添加一个“我的”文件夹,然后复制 Zends 类结构,例如:

Create the file创建文件

library/My/Controller/Action.php with the classname: My_Controller_Action library/My/Controller/Action.php,类名:My_Controller_Action

Make sure you include : autoloaderNamespaces.my = "My_" in your application.ini file, this includes the My directory to your autoloader.确保在 application.ini 文件中包含: autoloaderNamespaces.my = "My_",这包括自动加载器的 My 目录。

You should then be able to extend My_Controller_Action from anyone of your module controllers.然后,您应该能够从您的任何模块控制器扩展 My_Controller_Action。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM