Using a Conventional Modular Directory Structure粗略翻译

来源:互联网 发布:ubuntu apt get 路径 编辑:程序博客网 时间:2024/06/05 00:13

使用传统的模块目录结构

The Conventional Modular directory structure allows you to separate different MVC applications into self-contained units, and re-use them with different front controllers. To illustrate such a directory structure:

传统的模块目录结构允许您的MVC分离成不同的独立模块,并可以重用这些不同的前端控制器。目录结构如下:

  1. docroot/
  2.     index.php
  3. application/
  4.     default/
  5.         controllers/
  6.             IndexController.php
  7.             FooController.php
  8.         models/
  9.         views/
  10.             scripts/
  11.                 index/
  12.                 foo/
  13.             helpers/
  14.             filters/
  15.     blog/
  16.         controllers/
  17.             IndexController.php
  18.         models/
  19.         views/
  20.             scripts/
  21.                 index/
  22.             helpers/
  23.             filters/
  24.     news/
  25.         controllers/
  26.             IndexController.php
  27.             ListController.php
  28.         models/
  29.         views/
  30.             scripts/
  31.                 index/
  32.                 list/
  33.             helpers/
  34.             filters/
In this paradigm, the module name serves as a prefix to the controllers it contains. The above example contains three module controllers, 'Blog_IndexController', 'News_IndexController', and 'News_ListController'. Two global controllers, 'IndexController' and 'FooController' are also defined; neither of these will be namespaced. This directory structure will be used for examples in this chapter.

在这种模式下,模块名使用一个控制器作为名称。上面的例子包含三个模块控制器,'Blog_IndexController','News_IndexController'和'News_ListController'。

都被指定了命名空间。

两个全局控制器,'IndexController'和'FooController'。

这个目录结构将用于在本章的例子。

 

Note: No Namespacing in the Default Module
Note that in the default module, controllers do not need a namespace prefix. Thus, in the example above, the controllers in the default module do not need a prefix of 'Default_' -- they are simply dispatched according to their base controller name: 'IndexController' and 'FooController'. A namespace prefix is used in all other modules, however.

注意:没有命名空间的,使用缺省模块命名空间
请注意,在默认的模块,控制器不需要一个命名空间前缀。因此,在上面的例子,在缺省模块的控制器不需要的'Default_'前缀 - 他们只是根据自己的基地派遣控制器的名称:'IndexController'和'FooController'。命名空间前缀是用在所有其他模块。

 

So, how do you implement such a directory layout using the Zend Framework MVC components?

那么,你怎么让Zend Framework的MVC实现这样的目录结构呢?

Specifying Module Controller Directories

设置模块控制器目录

 

he first step to making use of modules is to modify how you specify the controller directory list in the front controller. In the basic MVC series, you pass either an array or a string to setControllerDirectory(), or a path to addControllerDirectory(). When using modules, you need to alter your calls to these methods slightly.

第一步,使模块的使用方法是修改指定如何在前端控制器控制器目录清单。

在基本的MVC结构中,通过使用setControllerDirectory(),或addControllerDirectory()传递路径数组或字符串来完成。当使用模块化结构,你需要调用这些方法来实现。

With setControllerDirectory(), you will need to pass an associative array and specify key and value pairs of module name and directory paths. The special key default will be used for global controllers (those not needing a module namespace). All entries should contain a string key pointing to a single path, and the default key must be present. As an example:

setControllerDirectory(),可以传递一个指定模块名和目录路径键值对的关联数组。 如果没有键值,被用于当做全局控制器(即没有命名空间的)。所有传递的值必须包含一个字符串键指向一个单一的路径,即使是作为默认的控制器也要添加默认键值。例如

  1. $front->setControllerDirectory(array(
  2.     'default' => '/path/to/application/controllers',
  3.     'blog'    => '/path/to/application/blog/controllers'
  4. ));
addControllerDirectory() will take an optional second argument. When using modules, pass the module name as the second argument; if not specified, the path will be added to the default namespace. As an example:

 

addControllerDirectory()传递一个可选的参数。当采用模块化结构时,可以经模块名称作为第二个参数传入,如果没有指定,路径将被添加到默认的命名空间。

例如:

  1. $front->addControllerDirectory('/path/to/application/news/controllers',
  2.                                'news');

Saving the best for last, the easiest way to specify module directories is to do so en masse, with all modules under a common directory and sharing the same structure. This can be done withaddModuleDirectory():

可以这样用addModuleDirectory()批量完成路径设置:

  1. /**
  2. * Assuming the following directory structure:
  3. * application/
  4. *     modules/
  5. *         default/
  6. *             controllers/
  7. *         foo/
  8. *             controllers/
  9. *         bar/
  10. *             controllers/
  11. */
  12. $front->addModuleDirectory('/path/to/application/modules');

The above example will define the default, foo, and bar modules, each pointing to the controllers/subdirectory of their respective module.

You may customize the controller subdirectory to use within your modules by usingsetModuleControllerDirectoryName():

  1. /**
  2. * Change the controllers subdirectory to be 'con'
  3. * application/
  4. *     modules/
  5. *         default/
  6. *             con/
  7. *         foo/
  8. *             con/
  9. *         bar/
  10. *             con/
  11. */
  12. $front->setModuleControllerDirectoryName('con');
  13. $front->addModuleDirectory('/path/to/application/modules');

Note: You can indicate that no controller subdirectory be used for your modules by passing an empty value to setModuleControllerDirectoryName(). 

Routing to Modules

The default route in Zend_Controller_Router_Rewrite is an object of typeZend_Controller_Router_Route_Module. This route expects one of the following routing schemas:

  • :module/:controller/:action/*

  • :controller/:action/*

In other words, it will match a controller and action by themselves or with a preceding module. The rules for matching specify that a module will only be matched if a key of the same name exists in the controller directory array passed to the front controller and dispatcher.

Module or Global Default Controller

In the default router, if a controller was not specified in the URL, a default controller is used (IndexController, unless otherwise requested). With modular controllers, if a module has been specified but no controller, the dispatcher first looks for this default controller in the module path, and then falls back on the default controller found in the 'default', global, namespace.

If you wish to always default to the global namespace, set the $useDefaultControllerAlways parameter in the front controller:

  1. $front->setParam('useDefaultControllerAlways', true);

 

 

 

 


原创粉丝点击