Home > php教程 > PHP开发 > Zend Framework tutorial front-end controller Zend_Controller_Front usage detailed explanation

Zend Framework tutorial front-end controller Zend_Controller_Front usage detailed explanation

高洛峰
Release: 2017-01-03 13:19:19
Original
1076 people have browsed it

The example in this article describes the usage of Zend Framework tutorial front-end controller Zend_Controller_Front. Share it with everyone for your reference, as follows:

Main functions

The core mechanism of ZendFramework's MVC implementation is through the Zend_Controller_Front front-end controller, which is used to initialize the request environment, process requests, and route distribution. To complete the response operation, Zend_Controller_Front adopts the singleton mode, so an application has only one front-end controller. If you need the front-end controller to provide some special functions, you can inherit Zend_Controller_Front to customize the front-end controller.

Main method

getInstance()

is used to obtain the front-end controller instance. The only way to create a front controller object.

$front = Zend_Controller_Front::getInstance();
Copy after login

setControllerDirectory() and addControllerDirectory()

setControllerDirectory() sets the storage location of the action controller action controller class file. Parameters can be path strings or associative arrays.

For example:

//路径是相对于应用的/application目录下
// 字符串
$front->setControllerDirectory('../application/controllers');
// 关联数组
$front->setControllerDirectory(array(
  'default' => '../application/controllers',
  'blog'  => '../modules/blog/controllers',
  'news'  => '../modules/news/controllers',
));
// Add a 'foo' module directory:
$front->addControllerDirectory('../modules/foo/controllers', 'foo');
Copy after login

Note: If you use addControllerDirectory() without a module name, the directory will be set for the default module—— If the directory is already set, it is overwritten.

The current setting of the controller directory can be obtained through getControllerDirectory(); it will return an associative array of module/directory pairs.

addModuleDirectory() and getModuleDirectory()

One feature of the front-end controller is that you can define a module directory structure to create independent components, called "modules".

Each module is located in its own directory and has the same directory structure as the default module - for example, it has at least a "controllers" subdirectory and a "views" subdirectory as well as other application subdirectories.

addModuleDirectory() lets you pass a directory name that contains one or more module directories. They are then scanned and added to the front controller as controller directories.

Then, if you want to determine a specific module or the current module path, call getModuleDirectory(), optionally passing the module name to get the module directory.

dispatch()

dispatch(Zend_Controller_Request_Abstract $request = null, Zend_Controller_Response_Abstract $response = null) completes the heaviest work of the front-end controller. This method takes optional parameters request object and/or response object, allowing the developer to pass in custom objects for each.

If no request or response object is passed in, dispatch() will check the previously registered object and use it, and if not found, create a default object version (both of them use HTTP objects by default).

Similarly, dispatch() first checks the registered router and dispatcher objects, and if not found, instantiates their default versions.

The distribution process has three different events: Routing, Dispatching, and Response

Routing only occurs once. When dispatch() is called, the request object is used. value. Distribution occurs in a loop; a request may indicate multiple actions to be dispatched, or a controller or plugin may reset the request object, forcing additional actions to be dispatched. When all is completed, the front controller returns the response object.

run()

Zend_Controller_Front::run($path) is a static method with only one parameter, which is the path to the directory containing the controller. It first obtains the front-end controller instance through getInstance(), then registers the incoming path through setControllerDirectory(), and finally distributes it.

Basically, if there is no requirement to customize the front-end controller environment, run() is a very convenient method to establish the front-end controller environment.

Zend_Controller_Front::run('../application/controllers');
Copy after login

Environment accessor methods

In addition to the methods listed above, there are many accessor methods that can affect the front-end controller environment— — thus also affecting the environment of the class of the front controller delegate.

The resetInstance() method clears all current settings. Mainly used for testing, but it can also be used for instances where you wish to chain together multiple front controllers.

(set|get)DefaultControllerName() method can specify another name for the default controller (otherwise use 'index') and get the current value. They will proxy the distributor.

(set|get)DefaultAction() method can specify another name for the default action (otherwise use 'index'), and get the current value. They will proxy the distributor.

(set|get)Request() method specifies the request class or object used in the distribution process, and obtains the current request object. When setting the request object, you can pass in the name of a request class, and this method will load the class file and create an instance.

(set|get)Router() method specifies the router class or object used in the distribution process, and obtains the current object. When setting up a router, you can pass in the name of a router class and this method will load the class file and create an instance.

When obtaining a router object, first check whether there is one already. If not, create a default router instance (rewrite router). The

(set|get)BaseUrl() method specifies the base address (base URL) to be stripped when routing the request, and obtains the current value. This value will be provided to the router before routing.

(set|get)Dispatcher()方法指定分发过程中使用的分发器类或对象,以及获取当前对象。设定分发器对象时,可以传入一个分发器类的名字,该方法将加载类文件并创建实例。

获取分发器对象时,首先检查是否已有一个存在,如果没有,将创建一个默认的分发器实例。

(set|get)Response()方法指定分发过程中使用的响应类或对象,已经获取当前对象。设定响应对象时,可以传入一个响应类的名字,该方法将加载类文件并创建实例。

registerPlugin(Zend_Controller_Plugin_Abstract $plugin, $stackIndex = null)方法允许注册一个插件对象。通过设置可选参数$stackIndex,插件执行的顺序。

unregisterPlugin($plugin)方法移除插件对象。$plugin可以是一个插件对象或者代表移除插件类的字符串。

throwExceptions($flag)方法用来开启或者关闭分发过程中抛出异常的能力。默认的,异常引起并放置在响应对象中;开启throwExceptions()将覆盖这一行为。

returnResponse($flag)方法通知前端控制器是否从dispatch()中返回请求对象(true),否则自动发送响应对象(false—)。默认的,响应对象被自动发送(通过调用Zend_Controller_Response_Abstract::sendResponse());开启returnResponse()将覆盖这一行为。

返回响应对象的原因包括希望在发送响应前检查异常,记录响应的各种属性(例如消息头)等等。

前端控制器参数

介绍里曾提到前端控制器可以用作各种控制器组件的注册表。它通过一个"param"家族的方法来做到这些。这些方法允许通过前端控制器注册任意类型的数据 —— 对象和变量,可以在分发链中的任何时候获取。这些变量被传递到路由器,分发器,以及动作控制器。这些方法包括:

setParam($name, $value)方法设定值为$value的单个参数$name。
setParams(array $params)方法通过关联数组一次设定多个参数。
getParam($name)方法通过$name标识符获取单个参数。
getParams()方法一次获取整个参数列表。
clearParams()方法可以清空一个参数(传入单个字符串标识符),清空多个参数(传入字符串标识符数组),清空整个参数栈(不传入参数)。

有几个预定义的参数可供设定,它们在分发链中有特别的用途:

useDefaultControllerAlways用来提示 分发器遇到无法分发的请求时使用默认模块的默认控制器。这默认是关闭的。

阅读可能遭遇的MVC异常获得使用该设定的更详尽信息。

disableOutputBuffering用来提示 is used to hint to 分发器不使用输出缓冲来捕捉动作控制器产生的输出。默认的,分发器捕捉任何输出并追加到响应对象的主体内容。

noViewRenderer用来禁用ViewRenderer。设定该参数为true可以禁用该助手。

noErrorHandler 用来禁用错误处理器插件。设定该参数为true可以禁用该插件。

自定义前端控制器

要继承前端控制器,至少需要覆盖getInstance()方法:

class My_Controller_Front extends Zend_Controller_Front
{
  public static function getInstance()
  {
    if (null === self::$_instance) {
      self::$_instance = new self();
    }
    return self::$_instance;
  }
}
Copy after login

覆盖getInstance()保证后面调用Zend_Controller_Front::getInstance()会返回子类的实例,而不是Zend_Controller_Front实例,这对于一些可替换的路由器和视图助手非常有用。

通常不需要继承前端控制器,除非你需要增加新的功能(比如,一个插件自动加载器,或者一个方法来指定动作助手路径)。你想要改动的地方可能包括修改控制器目录的存储方式,使用的默认路由器以及分发器。

ZendFramewrok提供的默认前端控制器已经足够我们使用了,通过Bootstrap功能,完全没有必要手动编写代码改变Zend_Controller_Front的默认机制。所以通常情况下Zend_Controller_Front对于应用来说是不存在。如果需要使用Zend_Controller_Front提供的功能,通过Zend_Controller_Front::getInstance();获取实例即可。

希望本文所述对大家PHP程序设计有所帮助。

更多Zend Framework教程之前端控制器Zend_Controller_Front用法详解相关文章请关注PHP中文网!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template