


Zend Framework tutorial front-end controller Zend_Controller_Front usage detailed explanation
Jan 06, 2017 am 09:43 AMThe 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();
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:
//路徑是相對于應(yīng)用的/application目錄下 // 字符串 $front->setControllerDirectory('../application/controllers'); // 關(guān)聯(lián)數(shù)組 $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 that can affect the front-end controller environment— — and thus also affects the environment of the front controller delegate's class.
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).
(set|get)BaseUrl()方法指定路由請求時(shí)剝離(strip)的基地址(base URL),以及獲取當(dāng)前值。這個(gè)值將在路由前提供給路由器。
(set|get)Dispatcher()方法指定分發(fā)過程中使用的分發(fā)器類或?qū)ο螅约矮@取當(dāng)前對象。設(shè)定分發(fā)器對象時(shí),可以傳入一個(gè)分發(fā)器類的名字,該方法將加載類文件并創(chuàng)建實(shí)例。
獲取分發(fā)器對象時(shí),首先檢查是否已有一個(gè)存在,如果沒有,將創(chuàng)建一個(gè)默認(rèn)的分發(fā)器實(shí)例。
(set|get)Response()方法指定分發(fā)過程中使用的響應(yīng)類或?qū)ο螅呀?jīng)獲取當(dāng)前對象。設(shè)定響應(yīng)對象時(shí),可以傳入一個(gè)響應(yīng)類的名字,該方法將加載類文件并創(chuàng)建實(shí)例。
registerPlugin(Zend_Controller_Plugin_Abstract $plugin, $stackIndex = null)方法允許注冊一個(gè)插件對象。通過設(shè)置可選參數(shù)$stackIndex,插件執(zhí)行的順序。
unregisterPlugin($plugin)方法移除插件對象。$plugin可以是一個(gè)插件對象或者代表移除插件類的字符串。
throwExceptions($flag)方法用來開啟或者關(guān)閉分發(fā)過程中拋出異常的能力。默認(rèn)的,異常引起并放置在響應(yīng)對象中;開啟throwExceptions()將覆蓋這一行為。
returnResponse($flag)方法通知前端控制器是否從dispatch()中返回請求對象(true),否則自動發(fā)送響應(yīng)對象(false—)。默認(rèn)的,響應(yīng)對象被自動發(fā)送(通過調(diào)用Zend_Controller_Response_Abstract::sendResponse());開啟returnResponse()將覆蓋這一行為。
返回響應(yīng)對象的原因包括希望在發(fā)送響應(yīng)前檢查異常,記錄響應(yīng)的各種屬性(例如消息頭)等等。
前端控制器參數(shù)
介紹里曾提到前端控制器可以用作各種控制器組件的注冊表。它通過一個(gè)"param"家族的方法來做到這些。這些方法允許通過前端控制器注冊任意類型的數(shù)據(jù) —— 對象和變量,可以在分發(fā)鏈中的任何時(shí)候獲取。這些變量被傳遞到路由器,分發(fā)器,以及動作控制器。這些方法包括:
setParam($name, $value)方法設(shè)定值為$value的單個(gè)參數(shù)$name。
setParams(array $params)方法通過關(guān)聯(lián)數(shù)組一次設(shè)定多個(gè)參數(shù)。
getParam($name)方法通過$name標(biāo)識符獲取單個(gè)參數(shù)。
getParams()方法一次獲取整個(gè)參數(shù)列表。
clearParams()方法可以清空一個(gè)參數(shù)(傳入單個(gè)字符串標(biāo)識符),清空多個(gè)參數(shù)(傳入字符串標(biāo)識符數(shù)組),清空整個(gè)參數(shù)棧(不傳入?yún)?shù))。
有幾個(gè)預(yù)定義的參數(shù)可供設(shè)定,它們在分發(fā)鏈中有特別的用途:
useDefaultControllerAlways用來提示 分發(fā)器遇到無法分發(fā)的請求時(shí)使用默認(rèn)模塊的默認(rèn)控制器。這默認(rèn)是關(guān)閉的。
閱讀可能遭遇的MVC異常獲得使用該設(shè)定的更詳盡信息。
disableOutputBuffering用來提示 is used to hint to 分發(fā)器不使用輸出緩沖來捕捉動作控制器產(chǎn)生的輸出。默認(rèn)的,分發(fā)器捕捉任何輸出并追加到響應(yīng)對象的主體內(nèi)容。
noViewRenderer用來禁用ViewRenderer。設(shè)定該參數(shù)為true可以禁用該助手。
noErrorHandler 用來禁用錯誤處理器插件。設(shè)定該參數(shù)為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()保證后面調(diào)用Zend_Controller_Front::getInstance()會返回子類的實(shí)例,而不是Zend_Controller_Front實(shí)例,這對于一些可替換的路由器和視圖助手非常有用。
通常不需要繼承前端控制器,除非你需要增加新的功能(比如,一個(gè)插件自動加載器,或者一個(gè)方法來指定動作助手路徑)。你想要改動的地方可能包括修改控制器目錄的存儲方式,使用的默認(rèn)路由器以及分發(fā)器。
ZendFramewrok提供的默認(rèn)前端控制器已經(jīng)足夠我們使用了,通過Bootstrap功能,完全沒有必要手動編寫代碼改變Zend_Controller_Front的默認(rèn)機(jī)制。所以通常情況下Zend_Controller_Front對于應(yīng)用來說是不存在。如果需要使用Zend_Controller_Front提供的功能,通過Zend_Controller_Front::getInstance();獲取實(shí)例即可。
希望本文所述對大家PHP程序設(shè)計(jì)有所幫助。
更多Zend Framework教程之前端控制器Zend_Controller_Front用法詳解相關(guān)文章請關(guān)注PHP中文網(wǎng)!

Hot AI Tools

Undress AI Tool
Undress images for free

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)