Zend Framework创建自己的动作助手详解_php实例
本文实例讲述了Zend Framework创建自己的动作助手实现方法。分享给大家供大家参考,具体如下:
助手的抽象基类是Zend_Controller_Action_Helper_Abstract,如要定义自己的助手,需要继承此类。
类的源代码如下:
<?php /** * @see Zend_Controller_Action */ require_once 'Zend/Controller/Action.php'; abstract class Zend_Controller_Action_Helper_Abstract { /** * $_actionController * * @var Zend_Controller_Action $_actionController */ protected $_actionController = null; /** * @var mixed $_frontController */ protected $_frontController = null; /** * setActionController() * * @param Zend_Controller_Action $actionController * @return Zend_Controller_ActionHelper_Abstract Provides a fluent interface */ public function setActionController(Zend_Controller_Action $actionController = null) { $this->_actionController = $actionController; return $this; } /** * Retrieve current action controller * * @return Zend_Controller_Action */ public function getActionController() { return $this->_actionController; } /** * Retrieve front controller instance * * @return Zend_Controller_Front */ public function getFrontController() { return Zend_Controller_Front::getInstance(); } /** * Hook into action controller initialization * * @return void */ public function init() { } /** * Hook into action controller preDispatch() workflow * * @return void */ public function preDispatch() { } /** * Hook into action controller postDispatch() workflow * * @return void */ public function postDispatch() { } /** * getRequest() - * * @return Zend_Controller_Request_Abstract $request */ public function getRequest() { $controller = $this->getActionController(); if (null === $controller) { $controller = $this->getFrontController(); } return $controller->getRequest(); } /** * getResponse() - * * @return Zend_Controller_Response_Abstract $response */ public function getResponse() { $controller = $this->getActionController(); if (null === $controller) { $controller = $this->getFrontController(); } return $controller->getResponse(); } /** * getName() * * @return string */ public function getName() { $fullClassName = get_class($this); if (strpos($fullClassName, '_') !== false) { $helperName = strrchr($fullClassName, '_'); return ltrim($helperName, '_'); } elseif (strpos($fullClassName, '\\') !== false) { $helperName = strrchr($fullClassName, '\\'); return ltrim($helperName, '\\'); } else { return $fullClassName; } } }
助手基类提供的常用方法如下:
setActionController() 用来设置当前的动作控制器。
init(),该方法在实例化时由助手经纪人触发,可用来触发助手的初始化过程;
动作链中多个控制器使用相同的助手时,如要恢复状态时将十分有用。
preDispatch()分发动作之前触发。
postDispatch()分发过程结束时触发——即使preDispatch()插件已经跳过了该动作。清理时大量使用。
getRequest() 获取当前的请求对象。
getResponse() 获取当前的响应对象。
getName() 获取助手名。获取了下划线后面的类名部分,没有下划线则获取类的全名。
例如,如果类名为Zend_Controller_Action_Helper_Redirector,他将返回 Redirector,如果类名为FooMessage,将会返回全名。
举例说明自定义动作助手类
作用:解析传入的网址,返回各个部分。使用parse_url解析指定的网址。
用zendstudio新建一个zend framework项目helper_demo1。
新增文件:/helper_demo1/library/Application/Controller/Action/Helpers/UrlParser.php
<?php require_once 'Zend/Controller/Action/Helper/Abstract.php'; class Application_Controller_Action_Helpers_UrlParser extends Zend_Controller_Action_Helper_Abstract { public function __construct() { } /** * Parse url * * @param String $url * @return Array part of url */ public function parse($url) { return parse_url($url); } }
修改文件:/helper_demo1/application/Bootstrap.php
<?php class Bootstrap extends Zend_Application_Bootstrap_Bootstrap { protected function _initAutoload() { $autoloader = Zend_Loader_Autoloader::getInstance(); $autoloader->registerNamespace(array('Application_')); } protected function _initActionHelpers() { //用前缀形式 //Zend_Controller_Action_HelperBroker::addPrefix('Application_Controller_Action_Helpers'); //指定目录和前缀 //Zend_Controller_Action_HelperBroker::addPath('/www/helper_demo1/library/Application/Controller/Action/Helpers', // 'Application_Controller_Action_Helpers'); //new一个助手类传入 Zend_Controller_Action_HelperBroker::addHelper(new Application_Controller_Action_Helpers_UrlParser); } }
修改测试action:/helper_demo1/application/controllers/IndexController.php
<?php class IndexController extends Zend_Controller_Action { public function init() { /* Initialize action controller here */ } public function indexAction() { $urlParser = $this->_helper->getHelper('UrlParser'); var_dump($urlParser->parse('http://www.php.net/article/80479.htm')); } }
以上介绍了自定义动作助手类,以及简单的使用方法。
需要注意的就是什么是助手类的前缀,助手类的名称以及助手的路径。
更多关于zend相关内容感兴趣的读者可查看本站专题:《Zend FrameWork框架入门教程》、《php优秀开发框架总结》、《Yii框架入门及常用技巧总结》、《ThinkPHP入门教程》、《php面向对象程序设计入门教程》、《php+mysql数据库操作入门教程》及《php常见数据库操作技巧汇总》
希望本文所述对大家PHP程序设计有所帮助。

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

AI Hentai Generator
Generate AI Hentai for free.

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

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

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

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
