本文实例讲述了Zend Framework教程之分发器Zend_Controller_Dispatcher用法。分享给大家供大家参考,具体如下:
分发器的具体实现
Zend Framework的分发器Zend_Controller_Dispatcher设计主要有,如下类和接口组成:
├── Dispatcher
│ ├── Abstract.php
│ ├── Exception.php
│ ├── Interface.php
│ └── Standard.php
Zend_Controller_Dispatcher_Interface
定义了分发器提供的基本和标准功能。
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
interface Zend_Controller_Dispatcher_Interface
{
public function formatControllerName( $unformatted );
public function formatModuleName( $unformatted );
public function formatActionName( $unformatted );
public function isDispatchable(Zend_Controller_Request_Abstract $request );
public function setParam( $name , $value );
public function setParams( array $params );
public function getParam( $name );
public function getParams();
public function clearParams( $name = null);
public function setResponse(Zend_Controller_Response_Abstract $response = null);
public function getResponse();
public function addControllerDirectory( $path , $args = null);
public function setControllerDirectory( $path );
public function getControllerDirectory();
public function dispatch(Zend_Controller_Request_Abstract $request , Zend_Controller_Response_Abstract $response );
public function isValidModule( $module );
public function getDefaultModule();
public function getDefaultControllerName();
public function getDefaultAction();
}
|
Zend_Controller_Dispatcher_Abstract
实现了Zend_Controller_Dispatcher_Interface接口,提供了分发器提供的基本和标准功能的抽象父类。
?
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
|
<?php
/** Zend_Controller_Dispatcher_Interface */
require_once 'Zend/Controller/Dispatcher/Interface.php' ;
abstract class Zend_Controller_Dispatcher_Abstract implements Zend_Controller_Dispatcher_Interface
{
protected $_defaultAction = 'index' ;
protected $_defaultController = 'index' ;
protected $_defaultModule = 'default' ;
protected $_frontController ;
protected $_invokeParams = array ();
protected $_pathDelimiter = '_' ;
protected $_response = null;
protected $_wordDelimiter = array ( '-' , '.' );
public function __construct( array $params = array ())
{
$this ->setParams( $params );
}
public function formatControllerName( $unformatted )
{
return ucfirst( $this ->_formatName( $unformatted )) . 'Controller' ;
}
public function formatActionName( $unformatted )
{
$formatted = $this ->_formatName( $unformatted , true);
return strtolower ( substr ( $formatted , 0, 1)) . substr ( $formatted , 1) . 'Action' ;
}
public function _verifyDelimiter( $spec )
{
if ( is_string ( $spec )) {
return ( array ) $spec ;
} elseif ( is_array ( $spec )) {
$allStrings = true;
foreach ( $spec as $delim ) {
if (! is_string ( $delim )) {
$allStrings = false;
break ;
}
}
if (! $allStrings ) {
require_once 'Zend/Controller/Dispatcher/Exception.php' ;
throw new Zend_Controller_Dispatcher_Exception( 'Word delimiter array must contain only strings' );
}
return $spec ;
}
require_once 'Zend/Controller/Dispatcher/Exception.php' ;
throw new Zend_Controller_Dispatcher_Exception( 'Invalid word delimiter' );
}
public function getWordDelimiter()
{
return $this ->_wordDelimiter;
}
public function setWordDelimiter( $spec )
{
$spec = $this ->_verifyDelimiter( $spec );
$this ->_wordDelimiter = $spec ;
return $this ;
}
public function getPathDelimiter()
{
return $this ->_pathDelimiter;
}
public function setPathDelimiter( $spec )
{
if (! is_string ( $spec )) {
require_once 'Zend/Controller/Dispatcher/Exception.php' ;
throw new Zend_Controller_Dispatcher_Exception( 'Invalid path delimiter' );
}
$this ->_pathDelimiter = $spec ;
return $this ;
}
protected function _formatName( $unformatted , $isAction = false)
{
// preserve directories
if (! $isAction ) {
$segments = explode ( $this ->getPathDelimiter(), $unformatted );
} else {
$segments = ( array ) $unformatted ;
}
foreach ( $segments as $key => $segment ) {
$segment = str_replace ( $this ->getWordDelimiter(), ' ' , strtolower ( $segment ));
$segment = preg_replace( '/[^a-z0-9 ]/' , '' , $segment );
$segments [ $key ] = str_replace ( ' ' , '' , ucwords( $segment ));
}
return implode( '_' , $segments );
}
public function getFrontController()
{
if (null === $this ->_frontController) {
require_once 'Zend/Controller/Front.php' ;
$this ->_frontController = Zend_Controller_Front::getInstance();
}
return $this ->_frontController;
}
public function setFrontController(Zend_Controller_Front $controller )
{
$this ->_frontController = $controller ;
return $this ;
}
public function setParam( $name , $value )
{
$name = (string) $name ;
$this ->_invokeParams[ $name ] = $value ;
return $this ;
}
public function setParams( array $params )
{
$this ->_invokeParams = array_merge ( $this ->_invokeParams, $params );
return $this ;
}
public function getParam( $name )
{
if (isset( $this ->_invokeParams[ $name ])) {
return $this ->_invokeParams[ $name ];
}
return null;
}
public function getParams()
{
return $this ->_invokeParams;
}
public function clearParams( $name = null)
{
if (null === $name ) {
$this ->_invokeParams = array ();
} elseif ( is_string ( $name ) && isset( $this ->_invokeParams[ $name ])) {
unset( $this ->_invokeParams[ $name ]);
} elseif ( is_array ( $name )) {
foreach ( $name as $key ) {
if ( is_string ( $key ) && isset( $this ->_invokeParams[ $key ])) {
unset( $this ->_invokeParams[ $key ]);
}
}
}
return $this ;
}
public function setResponse(Zend_Controller_Response_Abstract $response = null)
{
$this ->_response = $response ;
return $this ;
}
public function getResponse()
{
return $this ->_response;
}
public function setDefaultControllerName( $controller )
{
$this ->_defaultController = (string) $controller ;
return $this ;
}
public function getDefaultControllerName()
{
return $this ->_defaultController;
}
public function setDefaultAction( $action )
{
$this ->_defaultAction = (string) $action ;
return $this ;
}
public function getDefaultAction()
{
return $this ->_defaultAction;
}
public function setDefaultModule( $module )
{
$this ->_defaultModule = (string) $module ;
return $this ;
}
public function getDefaultModule()
{
return $this ->_defaultModule;
}
}
|
Zend_Controller_Dispatcher_Standard
ZendFramework继承抽象类Zend_Controller_Dispatcher_Abstract,定义了Zend_Controller_Dispatcher_Standard。Zend_Controller_Dispatcher_Standard是ZendFramework提供的基本的分发器,完成了分发功能。
?
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
|