Home>Article>Backend Development> Usage of Zend_Controller_Front in Zend Framework

Usage of Zend_Controller_Front in Zend Framework

Michael Jordan
Michael Jordan Original
2018-06-08 15:40:47 1115browse

This article mainly introduces the usage of the front-end controller Zend_Controller_Front in the Zend Framework tutorial, and analyzes in detail the functions, usage methods and related precautions of the front-end controller Zend_Controller_Front. Friends in need can refer to the following

Examples of this article Describes the usage of Zend Framework tutorial front-end controller Zend_Controller_Front. Share it with everyone for your reference, the details are 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. Processing requests, routing distribution, and completing response operations. 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();

setControllerDirectory() and addControllerDirectory()

setControllerDirectory() sets the action controller action controller class file Storage location. 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');

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');

Environment accessor methods

In addition to the methods listed above, there are many accessor methods available Affects the front controller environment - and thus the environment of the class that the front controller delegates to.

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()方法指定分发过程中使用的路由器类或对象,以及获取当前对象。设置路由器时,可以传入一个路由器类的名字,该方法将加载类文件并创建实例。

获取路由器对象的时候,首先检查是否已有一个,如果没有,创建默认的路由器实例(rewrite路由器)。

(set|get)BaseUrl()方法指定路由请求时剥离(strip)的基地址(base URL),以及获取当前值。这个值将在路由前提供给路由器。

(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; } }

覆盖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 Zend_Db database operations in Framework

The above is the detailed content of Usage of Zend_Controller_Front in Zend Framework. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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