本文實例講述了Zend Framework創(chuàng)建自己的動作助手實現(xiàn)方法。分享給大家供大家參考,具體如下:
助手的抽象基類是Zend_Controller_Action_Helper_Abstract,如要定義自己的助手,需要繼承此類。
類的源代碼如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
|
<?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() 用來設(shè)置當(dāng)前的動作控制器。
init(),該方法在實例化時由助手經(jīng)紀(jì)人觸發(fā),可用來觸發(fā)助手的初始化過程;
動作鏈中多個控制器使用相同的助手時,如要恢復(fù)狀態(tài)時將十分有用。
preDispatch()分發(fā)動作之前觸發(fā)。
postDispatch()分發(fā)過程結(jié)束時觸發(fā)——即使preDispatch()插件已經(jīng)跳過了該動作。清理時大量使用。
getRequest() 獲取當(dāng)前的請求對象。
getResponse() 獲取當(dāng)前的響應(yīng)對象。
getName() 獲取助手名。獲取了下劃線后面的類名部分,沒有下劃線則獲取類的全名。
例如,如果類名為Zend_Controller_Action_Helper_Redirector,他將返回 Redirector,如果類名為FooMessage,將會返回全名。
舉例說明自定義動作助手類
作用:解析傳入的網(wǎng)址,返回各個部分。使用parse_url解析指定的網(wǎng)址。
用zendstudio新建一個zend framework項目helper_demo1。
新增文件:/helper_demo1/library/Application/Controller/Action/Helpers/UrlParser.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
<?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
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
<?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
1
2
3
4
5
6
7
8
9
10
11
12
13
|
<?php class IndexController extends Zend_Controller_Action { public function init() { /* Initialize action controller here */ } public function indexAction() { $urlParser = $this ->_helper->getHelper( 'UrlParser' ); } } |
以上介紹了自定義動作助手類,以及簡單的使用方法。
需要注意的就是什么是助手類的前綴,助手類的名稱以及助手的路徑。
希望本文所述對大家PHP程序設(shè)計有所幫助。