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 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548
|
<sect1 id="zend.controller.front">
<title>Az elülső vezérlő</title>
<sect2 id="zend.controller.front.overview">
<title>Áttekintés</title>
<para>
A
<code>Zend_Controller_Front</code>
egy
<ulink url="http://hu.wikipedia.org/wiki/Modell-nézet-vezérlő">Modell-Nézet-Vezérlő</ulink>
(MNV) alkalmazásokban használatos
<ulink url="http://www.martinfowler.com/eaaCatalog/frontController.html">Elülső Vezérlő</ulink>
mintát valósít meg. Feladata, hogy előkészítse a kéréskörnyezetet, irányítsa a bejövő kérést, és kézbesítsen minden felfedezett műveletnek; összegyűjt minden választ és visszatér velük, amikor a folyamat befejeződik.
</para>
<para>
A
<code>Zend_Controller_Front</code>
megvalósítja az
<ulink url="http://en.wikipedia.org/wiki/Singleton_pattern">Egyke</ulink>
mintát is, ami annyit tesz, hogy mindig csak egy példány lehet belőle elérhető. Ez lehetővé teszi, hogy egy a kézbesítési folyamat más objektumai által igénybevehető nyílvántartásként is működjön.
</para>
<para>
A
<code>Zend_Controller_Front</code>
bejegyez magával egy
<link linkend="zend.controller.plugins">bővítménykezelőt</link>,
lehetővé téve, hogy bővítmények figyelhessék a különböző eseményeket, amiket kivált. Ez a legtöbb esetben biztosítja a fejlesztőnek a lehetőséget, hogy a célnak megfelelően alakítsa a kézbesítési folyamatot, anélkül, hogy funkcionalitás hozzáadásához ki kelljen terjesztenie az elülső vezérlőt.
</para>
<para>
Munkája elvégzése érdekében az elülső vezérlőnek legkevesebb egy
<link linkend="zend.controller.action">műveletvezérlőket</link>
tartalmazó könyvtár elérési útjára van szüksége. Különféle más tagfüggvények is meghívhatók az elülső vezérlő és segéd osztályai környezetének további testreszabásához.
</para>
<note>
<title>Alapértelmezett viselkedés</title>
<para>
Alapból az elülső vezérlő betölti az
<link linkend="zend.controller.plugins.standard.errorhandler">ErrorHandler</link>
bővítményt, akárcsak a
<link linkend="zend.controller.actionhelpers.viewrenderer">ViewRenderer</link>
műveletsegéd bővítményt. Ezek a hibakezelés és a nézet megjelenítés egyszerűsítésére vannak, ebben a sorrendben.
</para>
<para>
Az
<code>ErrorHandler</code>
kikapcsolásához hajtsd végre a következőt, bármikor a
<code>dispatch()</code>
meghívása előtt:
</para>
<programlisting role="php"><![CDATA[<?php
//Az ErrorHandler bővítmény kikapcsolása:
$front->setParam('noErrorHandler', true);]]>
</programlisting>
<para>
A
<code>ViewRenderer</code>
kikapcsolásához tedd a következőt a
<code>dispatch()</code>
hívása előtt:
</para>
<programlisting role="php"><![CDATA[<?php
//A ViewRenderer segéd kikapcsolása:
$front->setParam('noViewRenderer', true);]]>
</programlisting>
</note>
</sect2>
<sect2 id="zend.controller.front.methods.primary">
<title>Primary Methods</title>
<para>
The front controller has several accessors for setting up its
environment. However, there are three primary methods key to the
front controller's functionality:
</para>
<sect3 id="zend.controller.front.methods.primary.getinstance">
<title>getInstance()</title>
<para>
<code>getInstance()</code> is used to retrieve a front
controller instance. As the front controller implements a
Singleton pattern, this is also the only means possible for
instantiating a front controller object.
</para>
<programlisting role="php"><![CDATA[<?php
$front = Zend_Controller_Front::getInstance();]]>
</programlisting>
</sect3>
<sect3 id="zend.controller.front.methods.primary.setcontrollerdirectory">
<title>setControllerDirectory() and addControllerDirectory</title>
<para>
<code>setControllerDirectory()</code> is used to tell <link
linkend="zend.controller.dispatcher">the dispatcher</link>
where to look for <link
linkend="zend.controller.action">action controller</link>
class files. It accepts either a single path or an associative
array of module/path pairs.
</para>
<para>
As some examples:
</para>
<programlisting role="php"><![CDATA[
// Set the default controller directory:
$front->setControllerDirectory('../application/controllers');
// Set several module directories at once:
$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');]]>
</programlisting>
<note>
<para>
If you use <code>addControllerDirectory()</code> without a
module name, it will set the directory for the
<code>default</code> module -- overwriting it if it already
exists.
</para>
</note>
<para>
You can get the current settings for the controller directory
using <code>getControllerDirectory()</code>; this will return an
array of module/directory pairs.
</para>
</sect3>
<sect3 id="zend.controller.front.methods.primary.dispatch">
<title>dispatch()</title>
<para>
<code>dispatch(Zend_Controller_Request_Abstract $request = null,
Zend_Controller_Response_Abstract $response = null)</code>
does the heavy work of the front controller. It may optionally
take a <link linkend="zend.controller.request">request
object</link> and/or a <link
linkend="zend.controller.response">response object</link>,
allowing the developer to pass in custom objects for each.
</para>
<para>
If no request or response object are passed in,
<code>dispatch()</code> will check for previously registered
objects and use those or instantiate default versions to use in
its process (in both cases, the HTTP flavor will be used as the
default).
</para>
<para>
Similarly, <code>dispatch()</code> checks for registered <link
linkend="zend.controller.router">router</link> and <link
linkend="zend.controller.dispatcher">dispatcher</link>
objects, instantiating the default versions of each if none is
found.
</para>
<para>
The dispatch process has three distinct events:
</para>
<itemizedlist>
<listitem><para>Routing</para></listitem>
<listitem><para>Dispatching</para></listitem>
<listitem><para>Response</para></listitem>
</itemizedlist>
<para>
Routing takes place exactly once, using the values in the
request object when <code>dispatch()</code> is called.
Dispatching takes place in a loop; a request may either indicate
multiple actions to dispatch, or the controller or a plugin may
reset the request object to force additional actions to
dispatch. When all is done, the front controller returns a
response.
</para>
</sect3>
<sect3 id="zend.controller.front.methods.primary.run">
<title>run()</title>
<para>
<code>Zend_Controller_Front::run($path)</code> is a static
method taking simply a path to a directory containing
controllers. It fetches a front controller instance (via
<link
linkend="zend.controller.front.methods.primary.getinstance">getInstance()</link>),
registers the path provided via <link
linkend="zend.controller.front.methods.primary.setcontrollerdirectory">setControllerDirectory()</link>,
and finally <link
linkend="zend.controller.front.methods.primary.dispatch">dispatches</link>.
</para>
<para>
Basically, <code>run()</code> is a convenience method that can
be used for site setups that do not require customization of the
front controller environment.
</para>
<programlisting role="php"><![CDATA[<?php
// Instantiate front controller, set controller directory, and dispatch in one
// easy step:
Zend_Controller_Front::run('../application/controllers');]]>
</programlisting>
</sect3>
</sect2>
<sect2 id="zend.controller.front.methods.environment">
<title>Environmental Accessor Methods</title>
<para>
In addition to the methods listed above, there are a number of
accessor methods that can be used to affect the front controller
environment -- and thus the environment of the classes to which the
front controller delegates.
</para>
<itemizedlist>
<listitem>
<para>
<code>resetInstance()</code> can be used to clear all
current settings. Its primary purpose is for testing, but it
can also be used for instances where you wish to chain
together multiple front controllers.
</para>
</listitem>
<listitem>
<para>
<code>(set|get)DefaultControllerName()</code> let you
specify a different name to use for the default controller
('index' is used otherwise) and retrieve the current value.
They proxy to <link
linkend="zend.controller.dispatcher">the
dispatcher</link>.
</para>
</listitem>
<listitem>
<para>
<code>(set|get)DefaultActionName()</code> let you specify a
different name to use for the default action ('index' is
used otherwise) and retrieve the current value. They proxy
to <link linkend="zend.controller.dispatcher">the
dispatcher</link>.
</para>
</listitem>
<listitem>
<para>
<code>(set|get)Request()</code> let you specify <link
linkend="zend.controller.request">the request</link>
class or object to use during the dispatch process and to
retrieve the current object. When setting the request
object, you may pass in a request class name, in which case
the method will load the class file and instantiate it.
</para>
</listitem>
<listitem>
<para>
<code>(set|get)Router()</code> let you specify <link
linkend="zend.controller.router">the router</link>
class or object to use during the dispatch process and to
retrieve the current object. When setting the router
object, you may pass in a router class name, in which case
the method will load the class file and instantiate it.
</para>
<para>
When retrieving the router object, it first checks to see if
one is present, and if not, instantiates the default router
(rewrite router).
</para>
</listitem>
<listitem>
<para>
<code>(set|get)BaseUrl()</code> let you specify <link
linkend="zend.controller.request.http.baseurl">the base
URL</link> to strip when routing requests and to
retrieve the current value. The value is provided to the
request object just prior to routing.
</para>
</listitem>
<listitem>
<para>
<code>(set|get)Dispatcher()</code> let you specify <link
linkend="zend.controller.dispatcher">the
dispatcher</link> class or object to use during the
dispatch process and retrieve the current object. When
setting the dispatcher object, you may pass in a dispatcher
class name, in which case the method will load the class
file and instantiate it.
</para>
<para>
When retrieving the dispatcher object, it first checks to see if
one is present, and if not, instantiates the default
dispatcher.
</para>
</listitem>
<listitem>
<para>
<code>(set|get)Response()</code> let you specify <link
linkend="zend.controller.response">the response</link>
class or object to use during the dispatch process and to
retrieve the current object. When setting the response
object, you may pass in a response class name, in which case
the method will load the class file and instantiate it.
</para>
</listitem>
<listitem>
<para>
<code>registerPlugin(Zend_Controller_Plugin_Abstract $plugin, $stackIndex = null)</code>
allows you to register a <link
linkend="zend.controller.plugins">plugin objects</link>.
By setting the optional <code>$stackIndex</code>, you can
control the order in which plugins will execute.
</para>
</listitem>
<listitem>
<para>
<code>unregisterPlugin($plugin)</code> let you
unregister <link
linkend="zend.controller.plugins">plugin objects</link>.
<code>$plugin</code> may be either a plugin object or a
string denoting the class of plugin to unregister.
</para>
</listitem>
<listitem>
<para>
<code>throwExceptions($flag)</code> is used to turn on/off
the ability to throw exceptions during the dispatch process.
By default, exceptions are caught and placed in the <link
linkend="zend.controller.response">response
object</link>; turning on <code>throwExceptions()</code>
will override this behaviour.
</para>
<para>
For more information, read <xref
linkend="zend.controller.exceptions" />.
</para>
</listitem>
<listitem>
<para>
<code>returnResponse($flag)</code> is used to tell the front
controller whether to return the response
(<code>true</code>) from <code>dispatch()</code>, or if the
response should be automatically emitted
(<code>false</code>). By default, the response is
automatically emitted (by calling
<code>Zend_Controller_Response_Abstract::sendResponse()</code>);
turning on <code>returnResponse()</code> will override this
behaviour.
</para>
<para>
Reasons to return the response include a desire to check for
exceptions prior to emitting the response, needing to log
various aspects of the response (such as headers), etc.
</para>
</listitem>
</itemizedlist>
</sect2>
<sect2 id="zend.controller.front.methods.params">
<title>Front Controller Parameters</title>
<para>
In the introduction, we indicated that the front controller also
acts as a registry for the various controller components. It does so
through a family of "param" methods. These methods allow you to
register arbitrary data -- objects and variables -- with the front
controller to be retrieved at any time in the dispatch chain. These
values are passed on to the router, dispatcher, and action
controllers. The methods include:
</para>
<itemizedlist>
<listitem>
<para>
<code>setParam($name, $value)</code> allows you to set a
single parameter of <code>$name</code> with value
<code>$value</code>.
</para>
</listitem>
<listitem>
<para>
<code>setParams(array $params)</code> allows you to set
multiple parameters at once using an associative array.
</para>
</listitem>
<listitem>
<para>
<code>getParam($name)</code> allows you to retrieve a single
parameter at a time, using <code>$name</code> as the
identifier.
</para>
</listitem>
<listitem>
<para>
<code>getParams()</code> allows you to retrieve the entire
list of parameters at once.
</para>
</listitem>
<listitem>
<para>
<code>clearParams()</code> allows you to clear a single
parameter (by passing a string identifier), multiple named
parameters (by passing an array of string identifiers), or the
entire parameter stack (by passing nothing).
</para>
</listitem>
</itemizedlist>
<para>
There are several pre-defined parameters that may be set that have
specific uses in the dispatch chain:
</para>
<itemizedlist>
<listitem>
<para>
<code>useDefaultControllerAlways</code> is used to hint to
<link linkend="zend.controller.dispatcher">the
dispatcher</link> to use the default controller in the
default module for any request that is not dispatchable
(i.e., the module, controller, and/or action do not exist).
By default, this is off.
</para>
<para>
See <xref linkend="zend.controller.exceptions.internal" />
for more detailed information on using this setting.
</para>
</listitem>
<listitem>
<para>
<code>disableOutputBuffering</code> is used to hint to <link
linkend="zend.controller.dispatcher">the
dispatcher</link> that it should not use output
buffering to capture output generated by action controllers.
By default, the dispatcher captures any output and appends
it to the response object body content.
</para>
</listitem>
<listitem>
<para>
<code>noViewRenderer</code> is used to disable the <link
linkend="zend.controller.actionhelpers.viewrenderer">ViewRenderer</link>.
Set this parameter to true to disable it.
</para>
</listitem>
<listitem>
<para>
<code>noErrorHandler</code> is used to disable the <link
linkend="zend.controller.plugins.standard.errorhandler">Error
Handler plugin</link>. Set this parameter to true to
disable it.
</para>
</listitem>
</itemizedlist>
</sect2>
<sect2 id="zend.controller.front.subclassing">
<title>Subclassing the Front Controller</title>
<para>
To subclass the Front Controller, at the very minimum you will need
to override the <code>getInstance()</code> method:
</para>
<programlisting role="php"><![CDATA[
class My_Controller_Front extends Zend_Controller_Front
{
public static function getInstance()
{
if (null === self::$_instance) {
self::$_instance = new self();
}
return self::$_instance;
}
}
]]>
</programlisting>
<para>
Overriding the <code>getInstance()</code> method ensures that
subsequent calls to
<code>Zend_Controller_Front::getInstance()</code> will return an
instance of your new subclass instead of a
<code>Zend_Controller_Front</code> instance -- this is particularly
useful for some of the alternate routers and view helpers.
</para>
<para>
Typically, you will not need to subclass the front controller unless
you need to add new functionality (for instance, a plugin
autoloader, or a way to specify action helper paths). Some points
where you may want to alter behaviour may include modifying how
controller directories are stored, or what default router or
dispatcher are used.
</para>
</sect2>
</sect1>
<!--
vim:se ts=4 sw=4 et:
-->
|