


Zend Framework tutorial front-end controller Zend_Controller_Front usage detailed explanation, ledcontroller controller
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 and related precautions of the front-end controller Zend_Controller_Front. Friends in need can refer to the
example of 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. 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 The location where the file is stored. 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 has been set, overwrite it.
The current setting of the controller directory can be obtained via 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 and other application subdirectories.
addModuleDirectory() lets you pass the name of a directory containing 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 create the default object version if none is found (both of them use HTTP objects by default).
Similarly, dispatch() first checks the registered router and dispatcher objects, and instantiates their default versions if not found.
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 complete, the front controller returns the response object.
run()
Zend_Controller_Front::run($path) is a static method that takes 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 accessors Methods can affect 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();获取实例即可。

Hot AI Tools

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

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

Undress AI Tool
Undress images for free

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)

Hot Topics

.NET Framework 4 is required by developers and end users to run the latest versions of applications on Windows. However, while downloading and installing .NET Framework 4, many users complained that the installer stopped midway, displaying the following error message - " .NET Framework 4 has not been installed because Download failed with error code 0x800c0006 ". If you are also experiencing it while installing .NETFramework4 on your device then you are at the right place

Whenever your Windows 11 or Windows 10 PC has an upgrade or update issue, you will usually see an error code indicating the actual reason behind the failure. However, sometimes confusion can arise when an upgrade or update fails without an error code being displayed. With handy error codes, you know exactly where the problem is so you can try to fix it. But since no error code appears, it becomes challenging to identify the issue and resolve it. This will take up a lot of your time to simply find out the reason behind the error. In this case, you can try using a dedicated tool called SetupDiag provided by Microsoft that helps you easily identify the real reason behind the error.
![SCNotification has stopped working [5 steps to fix it]](https://img.php.cn/upload/article/000/887/227/168433050522031.png?x-oss-process=image/resize,m_fill,h_207,w_330)
As a Windows user, you are likely to encounter SCNotification has stopped working error every time you start your computer. SCNotification.exe is a Microsoft system notification file that crashes every time you start your PC due to permission errors and network failures. This error is also known by its problematic event name. So you might not see this as SCNotification having stopped working, but as bug clr20r3. In this article, we will explore all the steps you need to take to fix SCNotification has stopped working so that it doesn’t bother you again. What is SCNotification.e

Microsoft Windows users who have installed Microsoft.NET version 4.5.2, 4.6, or 4.6.1 must install a newer version of the Microsoft Framework if they want Microsoft to support the framework through future product updates. According to Microsoft, all three frameworks will cease support on April 26, 2022. After the support date ends, the product will not receive "security fixes or technical support." Most home devices are kept up to date through Windows updates. These devices already have newer versions of frameworks installed, such as .NET Framework 4.8. Devices that are not updating automatically may

How to use ACL (AccessControlList) for permission control in Zend Framework Introduction: In a web application, permission control is a crucial function. It ensures that users can only access the pages and features they are authorized to access and prevents unauthorized access. The Zend framework provides a convenient way to implement permission control, using the ACL (AccessControlList) component. This article will introduce how to use ACL in Zend Framework

It's been a week since we talked about the new safe mode bug affecting users who installed KB5012643 for Windows 11. This pesky issue didn't appear on the list of known issues Microsoft posted on launch day, thus catching everyone by surprise. Well, just when you thought things couldn't get any worse, Microsoft drops another bomb for users who have installed this cumulative update. Windows 11 Build 22000.652 causes more problems So the tech company is warning Windows 11 users that they may experience problems launching and using some .NET Framework 3.5 applications. Sound familiar? But please don't be surprised

PHP implementation framework: ZendFramework introductory tutorial ZendFramework is an open source website framework developed by PHP and is currently maintained by ZendTechnologies. ZendFramework adopts the MVC design pattern and provides a series of reusable code libraries to serve the implementation of Web2.0 applications and Web Serve. ZendFramework is very popular and respected by PHP developers and has a wide range of

According to news on December 9, Cooler Master recently demonstrated a mini chassis kit in cooperation with notebook modular solution provider Framework at a demonstration event at the Taipei Compute Show. The unique thing about this kit is that it can be compatible with and Install the motherboard from the framework notebook. Currently, this product has begun to be sold on the market, priced at 39 US dollars, which is equivalent to approximately 279 yuan at the current exchange rate. The model number of this chassis kit is named "frameWORKMAINBOARDCASE". In terms of design, it embodies the ultimate compactness and practicality, measuring only 297x133x15 mm. Its original design is to be able to seamlessly connect to framework notebooks
