open/home/wallst/ftp/diamondesign/library/Zend/Controller/Front.php
Zend_Controller_Dispatcher_Standard->dispatch(Zend_Controller_Request_Http, Zend_Controller_Response_Http)
1
<?php
2 /**
3 * Zend Framework
4 *
5 * LICENSE
6 *
7 * This source file is subject to the new BSD license that is bundled
8 * with this package in the file LICENSE.txt.
9 * It is also available through the world-wide-web at this URL:
10 * http://framework.zend.com/license/new-bsd
11 * If you did not receive a copy of the license and are unable to
12 * obtain it through the world-wide-web, please send an email
13 * to license@zend.com so we can send you a copy immediately.
14 *
15 * @category Zend
16 * @package Zend_Controller
17 * @copyright Copyright (c) 2005-2008 Zend Technologies USA Inc. (http://www.zend.com)
18 * @license http://framework.zend.com/license/new-bsd New BSD License
19 */
20
21
22 /** Zend_Loader */
23 require_once 'Zend/Loader.php';
24
25 /** Zend_Controller_Action_HelperBroker */
26 require_once 'Zend/Controller/Action/HelperBroker.php';
27
28 /** Zend_Controller_Exception */
29 require_once 'Zend/Controller/Exception.php';
30
31 /** Zend_Controller_Plugin_Broker */
32 require_once 'Zend/Controller/Plugin/Broker.php';
33
34 /**
35 * @category Zend
36 * @package Zend_Controller
37 * @copyright Copyright (c) 2005-2008 Zend Technologies USA Inc. (http://www.zend.com)
38 * @license http://framework.zend.com/license/new-bsd New BSD License
39 */
40 class Zend_Controller_Front
41 {
42 /**
43 * Base URL
44 * @var string
45 */
46 protected $_baseUrl = null;
47
48 /**
49 * Directory|ies where controllers are stored
50 *
51 * @var string|array
52 */
53 protected $_controllerDir = null;
54
55 /**
56 * Instance of Zend_Controller_Dispatcher_Interface
57 * @var Zend_Controller_Dispatcher_Interface
58 */
59 protected $_dispatcher = null;
60
61 /**
62 * Singleton instance
63 *
64 * Marked only as protected to allow extension of the class. To extend,
65 * simply override {@link getInstance()}.
66 *
67 * @var Zend_Controller_Front
68 */
69 protected static $_instance = null;
70
71 /**
72 * Array of invocation parameters to use when instantiating action
73 * controllers
74 * @var array
75 */
76 protected $_invokeParams = array();
77
78 /**
79 * Subdirectory within a module containing controllers; defaults to 'controllers'
80 * @var string
81 */
82 protected $_moduleControllerDirectoryName = 'controllers';
83
84 /**
85 * Instance of Zend_Controller_Plugin_Broker
86 * @var Zend_Controller_Plugin_Broker
87 */
88 protected $_plugins = null;
89
90 /**
91 * Instance of Zend_Controller_Request_Abstract
92 * @var Zend_Controller_Request_Abstract
93 */
94 protected $_request = null;
95
96 /**
97 * Instance of Zend_Controller_Response_Abstract
98 * @var Zend_Controller_Response_Abstract
99 */
100 protected $_response = null;
101
102 /**
103 * Whether or not to return the response prior to rendering output while in
104 * {@link dispatch()}; default is to send headers and render output.
105 * @var boolean
106 */
107 protected $_returnResponse = false;
108
109 /**
110 * Instance of Zend_Controller_Router_Interface
111 * @var Zend_Controller_Router_Interface
112 */
113 protected $_router = null;
114
115 /**
116 * Whether or not exceptions encountered in {@link dispatch()} should be
117 * thrown or trapped in the response object
118 * @var boolean
119 */
120 protected $_throwExceptions = false;
121
122 /**
123 * Constructor
124 *
125 * Instantiate using {@link getInstance()}; front controller is a singleton
126 * object.
127 *
128 * Instantiates the plugin broker.
129 *
130 * @return void
131 */
132 protected function __construct()
133 {
134 $this->_plugins = new Zend_Controller_Plugin_Broker();
135 }
136
137 /**
138 * Enforce singleton; disallow cloning
139 *
140 * @return void
141 */
142 private function __clone()
143 {
144 }
145
146 /**
147 * Singleton instance
148 *
149 * @return Zend_Controller_Front
150 */
151 public static function getInstance()
152 {
153 if (null === self::$_instance) {
154 self::$_instance = new self();
155 }
156
157 return self::$_instance;
158 }
159
160 /**
161 * Resets all object properties of the singleton instance
162 *
163 * Primarily used for testing; could be used to chain front controllers.
164 *
165 * Also resets action helper broker, clearing all registered helpers.
166 *
167 * @return void
168 */
169 public function resetInstance()
170 {
171 $reflection = new ReflectionObject($this);
172 foreach ($reflection->getProperties() as $property) {
173 $name = $property->getName();
174 switch ($name) {
175 case '_instance':
176 break;
177 case '_controllerDir':
178 case '_invokeParams':
179 $this->{$name} = array();
180 break;
181 case '_plugins':
182 $this->{$name} = new Zend_Controller_Plugin_Broker();
183 break;
184 case '_throwExceptions':
185 case '_returnResponse':
186 $this->{$name} = false;
187 break;
188 case '_moduleControllerDirectoryName':
189 $this->{$name} = 'controllers';
190 break;
191 default:
192 $this->{$name} = null;
193 break;
194 }
195 }
196 Zend_Controller_Action_HelperBroker::resetHelpers();
197 }
198
199 /**
200 * Convenience feature, calls setControllerDirectory()->setRouter()->dispatch()
201 *
202 * In PHP 5.1.x, a call to a static method never populates $this -- so run()
203 * may actually be called after setting up your front controller.
204 *
205 * @param string|array $controllerDirectory Path to Zend_Controller_Action
206 * controller classes or array of such paths
207 * @return void
208 * @throws Zend_Controller_Exception if called from an object instance
209 */
210 public static function run($controllerDirectory)
211 {
212 self::getInstance()
213 ->setControllerDirectory($controllerDirectory)
214 ->dispatch();
215 }
216
217 /**
218 * Add a controller directory to the controller directory stack
219 *
220 * If $args is presented and is a string, uses it for the array key mapping
221 * to the directory specified.
222 *
223 * @param string $directory
224 * @param string $module Optional argument; module with which to associate directory. If none provided, assumes 'default'
225 * @return Zend_Controller_Front
226 * @throws Zend_Controller_Exception if directory not found or readable
227 */
228 public function addControllerDirectory($directory, $module = null)
229 {
230 $this->getDispatcher()->addControllerDirectory($directory, $module);
231 return $this;
232 }
233
234 /**
235 * Set controller directory
236 *
237 * Stores controller directory(ies) in dispatcher. May be an array of
238 * directories or a string containing a single directory.
239 *
240 * @param string|array $directory Path to Zend_Controller_Action controller
241 * classes or array of such paths
242 * @param string $module Optional module name to use with string $directory
243 * @return Zend_Controller_Front
244 */
245 public function setControllerDirectory($directory, $module = null)
246 {
247 $this->getDispatcher()->setControllerDirectory($directory, $module);
248 return $this;
249 }
250
251 /**
252 * Retrieve controller directory
253 *
254 * Retrieves:
255 * - Array of all controller directories if no $name passed
256 * - String path if $name passed and exists as a key in controller directory array
257 * - null if $name passed but does not exist in controller directory keys
258 *
259 * @param string $name Default null
260 * @return array|string|null
261 */
262 public function getControllerDirectory($name = null)
263 {
264 return $this->getDispatcher()->getControllerDirectory($name);
265 }
266
267 /**
268 * Remove a controller directory by module name
269 *
270 * @param string $module
271 * @return bool
272 */
273 public function removeControllerDirectory($module)
274 {
275 return $this->getDispatcher()->removeControllerDirectory($module);
276 }
277
278 /**
279 * Specify a directory as containing modules
280 *
281 * Iterates through the directory, adding any subdirectories as modules;
282 * the subdirectory within each module named after {@link $_moduleControllerDirectoryName}
283 * will be used as the controller directory path.
284 *
285 * @param string $path
286 * @return Zend_Controller_Front
287 */
288 public function addModuleDirectory($path)
289 {
290 try{
291 $dir = new DirectoryIterator($path);
292 }catch(Exception $e){
293 throw new Zend_Controller_Exception("Directory $path not readable");
294 }
295 foreach ($dir as $file) {
296 if ($file->isDot() || !$file->isDir()) {
297 continue;
298 }
299
300 $module = $file->getFilename();
301
302 // Don't use SCCS directories as modules
303 if (preg_match('/^[^a-z]/i', $module) || ('CVS' == $module)) {
304 continue;
305 }
306
307 $moduleDir = $file->getPathname() . DIRECTORY_SEPARATOR . $this->getModuleControllerDirectoryName();
308 $this->addControllerDirectory($moduleDir, $module);
309 }
310
311 return $this;
312 }
313
314 /**
315 * Return the path to a module directory (but not the controllers directory within)
316 *
317 * @param string $module
318 * @return string|null
319 */
320 public function getModuleDirectory($module = null)
321 {
322 if (null === $module) {
323 $request = $this->getRequest();
324 if (null !== $request) {
325 $module = $this->getRequest()->getModuleName();
326 }
327 if (empty($module)) {
328 $module = $this->getDispatcher()->getDefaultModule();
329 }
330 }
331
332 $controllerDir = $this->getControllerDirectory($module);
333
334 if ((null === $controllerDir) || !is_string($controllerDir)) {
335 return null;
336 }
337
338 return dirname($controllerDir);
339 }
340
341 /**
342 * Set the directory name within a module containing controllers
343 *
344 * @param string $name
345 * @return Zend_Controller_Front
346 */
347 public function setModuleControllerDirectoryName($name = 'controllers')
348 {
349 $this->_moduleControllerDirectoryName = (string) $name;
350
351 return $this;
352 }
353
354 /**
355 * Return the directory name within a module containing controllers
356 *
357 * @return string
358 */
359 public function getModuleControllerDirectoryName()
360 {
361 return $this->_moduleControllerDirectoryName;
362 }
363
364 /**
365 * Set the default controller (unformatted string)
366 *
367 * @param string $controller
368 * @return Zend_Controller_Front
369 */
370 public function setDefaultControllerName($controller)
371 {
372 $dispatcher = $this->getDispatcher();
373 $dispatcher->setDefaultControllerName($controller);
374 return $this;
375 }
376
377 /**
378 * Retrieve the default controller (unformatted string)
379 *
380 * @return string
381 */
382 public function getDefaultControllerName()
383 {
384 return $this->getDispatcher()->getDefaultControllerName();
385 }
386
387 /**
388 * Set the default action (unformatted string)
389 *
390 * @param string $action
391 * @return Zend_Controller_Front
392 */
393 public function setDefaultAction($action)
394 {
395 $dispatcher = $this->getDispatcher();
396 $dispatcher->setDefaultAction($action);
397 return $this;
398 }
399
400 /**
401 * Retrieve the default action (unformatted string)
402 *
403 * @return string
404 */
405 public function getDefaultAction()
406 {
407 return $this->getDispatcher()->getDefaultAction();
408 }
409
410 /**
411 * Set the default module name
412 *
413 * @param string $module
414 * @return Zend_Controller_Front
415 */
416 public function setDefaultModule($module)
417 {
418 $dispatcher = $this->getDispatcher();
419 $dispatcher->setDefaultModule($module);
420 return $this;
421 }
422
423 /**
424 * Retrieve the default module
425 *
426 * @return string
427 */
428 public function getDefaultModule()
429 {
430 return $this->getDispatcher()->getDefaultModule();
431 }
432
433 /**
434 * Set request class/object
435 *
436 * Set the request object. The request holds the request environment.
437 *
438 * If a class name is provided, it will instantiate it
439 *
440 * @param string|Zend_Controller_Request_Abstract $request
441 * @throws Zend_Controller_Exception if invalid request class
442 * @return Zend_Controller_Front
443 */
444 public function setRequest($request)
445 {
446 if (is_string($request)) {
447 Zend_Loader::loadClass($request);
448 $request = new $request();
449 }
450 if (!$request instanceof Zend_Controller_Request_Abstract) {
451 throw new Zend_Controller_Exception('Invalid request class');
452 }
453
454 $this->_request = $request;
455
456 return $this;
457 }
458
459 /**
460 * Return the request object.
461 *
462 * @return null|Zend_Controller_Request_Abstract
463 */
464 public function getRequest()
465 {
466 return $this->_request;
467 }
468
469 /**
470 * Set router class/object
471 *
472 * Set the router object. The router is responsible for mapping
473 * the request to a controller and action.
474 *
475 * If a class name is provided, instantiates router with any parameters
476 * registered via {@link setParam()} or {@link setParams()}.
477 *
478 * @param string|Zend_Controller_Router_Interface $router
479 * @throws Zend_Controller_Exception if invalid router class
480 * @return Zend_Controller_Front
481 */
482 public function setRouter($router)
483 {
484 if (is_string($router)) {
485 Zend_Loader::loadClass($router);
486 $router = new $router();
487 }
488
489 if (!$router instanceof Zend_Controller_Router_Interface) {
490 throw new Zend_Controller_Exception('Invalid router class');
491 }
492
493 $router->setFrontController($this);
494 $this->_router = $router;
495
496 return $this;
497 }
498
499 /**
500 * Return the router object.
501 *
502 * Instantiates a Zend_Controller_Router_Rewrite object if no router currently set.
503 *
504 * @return Zend_Controller_Router_Interface
505 */
506 public function getRouter()
507 {
508 if (null == $this->_router) {
509 require_once 'Zend/Controller/Router/Rewrite.php';
510 $this->setRouter(new Zend_Controller_Router_Rewrite());
511 }
512
513 return $this->_router;
514 }
515
516 /**
517 * Set the base URL used for requests
518 *
519 * Use to set the base URL segment of the REQUEST_URI to use when
520 * determining PATH_INFO, etc. Examples:
521 * - /admin
522 * - /myapp
523 * - /subdir/index.php
524 *
525 * Note that the URL should not include the full URI. Do not use:
526 * - http://example.com/admin
527 * - http://example.com/myapp
528 * - http://example.com/subdir/index.php
529 *
530 * If a null value is passed, this can be used as well for autodiscovery (default).
531 *
532 * @param string $base
533 * @return Zend_Controller_Front
534 * @throws Zend_Controller_Exception for non-string $base
535 */
536 public function setBaseUrl($base = null)
537 {
538 if (!is_string($base) && (null !== $base)) {
539 throw new Zend_Controller_Exception('Rewrite base must be a string');
540 }
541
542 $this->_baseUrl = $base;
543
544 if ((null !== ($request = $this->getRequest())) && (method_exists($request, 'setBaseUrl'))) {
545 $request->setBaseUrl($base);
546 }
547
548 return $this;
549 }
550
551 /**
552 * Retrieve the currently set base URL
553 *
554 * @return string
555 */
556 public function getBaseUrl()
557 {
558 $request = $this->getRequest();
559 if ((null !== $request) && method_exists($request, 'getBaseUrl')) {
560 return $request->getBaseUrl();
561 }
562
563 return $this->_baseUrl;
564 }
565
566 /**
567 * Set the dispatcher object. The dispatcher is responsible for
568 * taking a Zend_Controller_Dispatcher_Token object, instantiating the controller, and
569 * call the action method of the controller.
570 *
571 * @param Zend_Controller_Dispatcher_Interface $dispatcher
572 * @return Zend_Controller_Front
573 */
574 public function setDispatcher(Zend_Controller_Dispatcher_Interface $dispatcher)
575 {
576 $this->_dispatcher = $dispatcher;
577 return $this;
578 }
579
580 /**
581 * Return the dispatcher object.
582 *
583 * @return Zend_Controller_Dispatcher_Interface
584 */
585 public function getDispatcher()
586 {
587 /**
588 * Instantiate the default dispatcher if one was not set.
589 */
590 if (!$this->_dispatcher instanceof Zend_Controller_Dispatcher_Interface) {
591 require_once 'Zend/Controller/Dispatcher/Standard.php';
592 $this->_dispatcher = new Zend_Controller_Dispatcher_Standard();
593 }
594 return $this->_dispatcher;
595 }
596
597 /**
598 * Set response class/object
599 *
600 * Set the response object. The response is a container for action
601 * responses and headers. Usage is optional.
602 *
603 * If a class name is provided, instantiates a response object.
604 *
605 * @param string|Zend_Controller_Response_Abstract $response
606 * @throws Zend_Controller_Exception if invalid response class
607 * @return Zend_Controller_Front
608 */
609 public function setResponse($response)
610 {
611 if (is_string($response)) {
612 Zend_Loader::loadClass($response);
613 $response = new $response();
614 }
615 if (!$response instanceof Zend_Controller_Response_Abstract) {
616 throw new Zend_Controller_Exception('Invalid response class');
617 }
618
619 $this->_response = $response;
620
621 return $this;
622 }
623
624 /**
625 * Return the response object.
626 *
627 * @return null|Zend_Controller_Response_Abstract
628 */
629 public function getResponse()
630 {
631 return $this->_response;
632 }
633
634 /**
635 * Add or modify a parameter to use when instantiating an action controller
636 *
637 * @param string $name
638 * @param mixed $value
639 * @return Zend_Controller_Front
640 */
641 public function setParam($name, $value)
642 {
643 $name = (string) $name;
644 $this->_invokeParams[$name] = $value;
645 return $this;
646 }
647
648 /**
649 * Set parameters to pass to action controller constructors
650 *
651 * @param array $params
652 * @return Zend_Controller_Front
653 */
654 public function setParams(array $params)
655 {
656 $this->_invokeParams = array_merge($this->_invokeParams, $params);
657 return $this;
658 }
659
660 /**
661 * Retrieve a single parameter from the controller parameter stack
662 *
663 * @param string $name
664 * @return mixed
665 */
666 public function getParam($name)
667 {
668 if(isset($this->_invokeParams[$name])) {
669 return $this->_invokeParams[$name];
670 }
671
672 return null;
673 }
674
675 /**
676 * Retrieve action controller instantiation parameters
677 *
678 * @return array
679 */
680 public function getParams()
681 {
682 return $this->_invokeParams;
683 }
684
685 /**
686 * Clear the controller parameter stack
687 *
688 * By default, clears all parameters. If a parameter name is given, clears
689 * only that parameter; if an array of parameter names is provided, clears
690 * each.
691 *
692 * @param null|string|array single key or array of keys for params to clear
693 * @return Zend_Controller_Front
694 */
695 public function clearParams($name = null)
696 {
697 if (null === $name) {
698 $this->_invokeParams = array();
699 } elseif (is_string($name) && isset($this->_invokeParams[$name])) {
700 unset($this->_invokeParams[$name]);
701 } elseif (is_array($name)) {
702 foreach ($name as $key) {
703 if (is_string($key) && isset($this->_invokeParams[$key])) {
704 unset($this->_invokeParams[$key]);
705 }
706 }
707 }
708
709 return $this;
710 }
711
712 /**
713 * Register a plugin.
714 *
715 * @param Zend_Controller_Plugin_Abstract $plugin
716 * @param int $stackIndex Optional; stack index for plugin
717 * @return Zend_Controller_Front
718 */
719 public function registerPlugin(Zend_Controller_Plugin_Abstract $plugin, $stackIndex = null)
720 {
721 $this->_plugins->registerPlugin($plugin, $stackIndex);
722 return $this;
723 }
724
725 /**
726 * Unregister a plugin.
727 *
728 * @param string|Zend_Controller_Plugin_Abstract $plugin Plugin class or object to unregister
729 * @return Zend_Controller_Front
730 */
731 public function unregisterPlugin($plugin)
732 {
733 $this->_plugins->unregisterPlugin($plugin);
734 return $this;
735 }
736
737 /**
738 * Is a particular plugin registered?
739 *
740 * @param string $class
741 * @return bool
742 */
743 public function hasPlugin($class)
744 {
745 return $this->_plugins->hasPlugin($class);
746 }
747
748 /**
749 * Retrieve a plugin or plugins by class
750 *
751 * @param string $class
752 * @return false|Zend_Controller_Plugin_Abstract|array
753 */
754 public function getPlugin($class)
755 {
756 return $this->_plugins->getPlugin($class);
757 }
758
759 /**
760 * Retrieve all plugins
761 *
762 * @return array
763 */
764 public function getPlugins()
765 {
766 return $this->_plugins->getPlugins();
767 }
768
769 /**
770 * Set the throwExceptions flag and retrieve current status
771 *
772 * Set whether exceptions encounted in the dispatch loop should be thrown
773 * or caught and trapped in the response object.
774 *
775 * Default behaviour is to trap them in the response object; call this
776 * method to have them thrown.
777 *
778 * Passing no value will return the current value of the flag; passing a
779 * boolean true or false value will set the flag and return the current
780 * object instance.
781 *
782 * @param boolean $flag Defaults to null (return flag state)
783 * @return boolean|Zend_Controller_Front Used as a setter, returns object; as a getter, returns boolean
784 */
785 public function throwExceptions($flag = null)
786 {
787 if ($flag !== null) {
788 $this->_throwExceptions = (bool) $flag;
789 return $this;
790 }
791
792 return $this->_throwExceptions;
793 }
794
795 /**
796 * Set whether {@link dispatch()} should return the response without first
797 * rendering output. By default, output is rendered and dispatch() returns
798 * nothing.
799 *
800 * @param boolean $flag
801 * @return boolean|Zend_Controller_Front Used as a setter, returns object; as a getter, returns boolean
802 */
803 public function returnResponse($flag = null)
804 {
805 if (true === $flag) {
806 $this->_returnResponse = true;
807 return $this;
808 } elseif (false === $flag) {
809 $this->_returnResponse = false;
810 return $this;
811 }
812
813 return $this->_returnResponse;
814 }
815
816 /**
817 * Dispatch an HTTP request to a controller/action.
818 *
819 * @param Zend_Controller_Request_Abstract|null $request
820 * @param Zend_Controller_Response_Abstract|null $response
821 * @return void|Zend_Controller_Response_Abstract Returns response object if returnResponse() is true
822 */
823 public function dispatch(Zend_Controller_Request_Abstract $request = null, Zend_Controller_Response_Abstract $response = null)
824 {
825 if (!$this->getParam('noErrorHandler') && !$this->_plugins->hasPlugin('Zend_Controller_Plugin_ErrorHandler')) {
826 // Register with stack index of 100
827 require_once 'Zend/Controller/Plugin/ErrorHandler.php';
828 $this->_plugins->registerPlugin(new Zend_Controller_Plugin_ErrorHandler(), 100);
829 }
830
831 if (!$this->getParam('noViewRenderer') && !Zend_Controller_Action_HelperBroker::hasHelper('viewRenderer')) {
832 require_once 'Zend/Controller/Action/Helper/ViewRenderer.php';
833 Zend_Controller_Action_HelperBroker::getStack()->offsetSet(-80, new Zend_Controller_Action_Helper_ViewRenderer());
834 }
835
836 /**
837 * Instantiate default request object (HTTP version) if none provided
838 */
839 if (null !== $request) {
840 $this->setRequest($request);
841 } elseif ((null === $request) && (null === ($request = $this->getRequest()))) {
842 require_once 'Zend/Controller/Request/Http.php';
843 $request = new Zend_Controller_Request_Http();
844 $this->setRequest($request);
845 }
846
847 /**
848 * Set base URL of request object, if available
849 */
850 if (is_callable(array($this->_request, 'setBaseUrl'))) {
851 if (null !== $this->_baseUrl) {
852 $this->_request->setBaseUrl($this->_baseUrl);
853 }
854 }
855
856 /**
857 * Instantiate default response object (HTTP version) if none provided
858 */
859 if (null !== $response) {
860 $this->setResponse($response);
861 } elseif ((null === $this->_response) && (null === ($this->_response = $this->getResponse()))) {
862 require_once 'Zend/Controller/Response/Http.php';
863 $response = new Zend_Controller_Response_Http();
864 $this->setResponse($response);
865 }
866
867 /**
868 * Register request and response objects with plugin broker
869 */
870 $this->_plugins
871 ->setRequest($this->_request)
872 ->setResponse($this->_response);
873
874 /**
875 * Initialize router
876 */
877 $router = $this->getRouter();
878 $router->setParams($this->getParams());
879
880 /**
881 * Initialize dispatcher
882 */
883 $dispatcher = $this->getDispatcher();
884 $dispatcher->setParams($this->getParams())
885 ->setResponse($this->_response);
886
887 // Begin dispatch
888 try {
889 /**
890 * Route request to controller/action, if a router is provided
891 */
892
893 /**
894 * Notify plugins of router startup
895 */
896 $this->_plugins->routeStartup($this->_request);
897
898 $router->route($this->_request);
899
900 /**
901 * Notify plugins of router completion
902 */
903 $this->_plugins->routeShutdown($this->_request);
904
905 /**
906 * Notify plugins of dispatch loop startup
907 */
908 $this->_plugins->dispatchLoopStartup($this->_request);
909
910 /**
911 * Attempt to dispatch the controller/action. If the $this->_request
912 * indicates that it needs to be dispatched, move to the next
913 * action in the request.
914 */
915 do {
916 $this->_request->setDispatched(true);
917
918 /**
919 * Notify plugins of dispatch startup
920 */
921 $this->_plugins->preDispatch($this->_request);
922
923 /**
924 * Skip requested action if preDispatch() has reset it
925 */
926 if (!$this->_request->isDispatched()) {
927 continue;
928 }
929
930 /**
931 * Dispatch request
932 */
933 try {
934 $dispatcher->dispatch($this->_request, $this->_response);
935 } catch (Exception $e) {
936 if ($this->throwExceptions()) {
937 throw $e;
938 }
939 $this->_response->setException($e);
940 }
941
942 /**
943 * Notify plugins of dispatch completion
944 */
945 $this->_plugins->postDispatch($this->_request);
946 } while (!$this->_request->isDispatched());
947 } catch (Exception $e) {
948 if ($this->throwExceptions()) {
949 throw $e;
950 }
951
952 $this->_response->setException($e);
953 }
954
955 /**
956 * Notify plugins of dispatch loop completion
957 */
958 try {
959 $this->_plugins->dispatchLoopShutdown();
960 } catch (Exception $e) {
961 if ($this->throwExceptions()) {
962 throw $e;
963 }
964
965 $this->_response->setException($e);
966 }
967
968 if ($this->returnResponse()) {
969 return $this->_response;
970 }
971
972 $this->_response->sendResponse();
973 }
974 }
975
open/home/wallst/ftp/diamondesign/index.php
Zend_Controller_Front->dispatch()
1
<?php
2
3 function getmicrotime(){ list($usec, $sec) = explode(" ",microtime()); return ((float)$usec + (float)$sec); } $time_start = getmicrotime();
4
5 error_reporting(E_ALL);
6 date_default_timezone_set('Europe/Warsaw');
7
8 ini_set( 'include_path', './library/' );
9 ini_set('display_errors', 1);
10
11 set_include_path('.' . PATH_SEPARATOR . './library/Zend/'
12 . PATH_SEPARATOR . './application/models/'
13 . PATH_SEPARATOR . get_include_path());
14
15 include "Zend/Loader.php";
16 Zend_Loader::registerAutoload();
17
18 Zend_Session::start();
19
20 /* load Smarty plugin */
21 include "Fruwoc/View/Smarty.php";
22
23 // load configuration
24 $config = new Zend_Config_Ini('./application/config.ini.php', 'general');
25 $registry = Zend_Registry::getInstance();
26 $registry->set('config', $config);
27
28 $baza_start = getmicrotime();
29 // setup database
30 $db = Zend_Db::factory($config->db->adapter, $config->db->config->toArray());
31 Zend_Db_Table::setDefaultAdapter($db);
32 Zend_Registry::set('db', $db);
33 $db->getConnection();
34 // $db->query('SET NAMES utf8');
35 $baza_end = getmicrotime();
36
37 /* routers */
38
39 $router = new Zend_Controller_Router_Rewrite();
40
41 $router->addRoute('admin_action_id_status_var', new Zend_Controller_Router_Route('admin/:controller/:action/:id/:status/:var', array('module' => 'admin')));
42 $router->addRoute('orderr', new Zend_Controller_Router_Route('admin/:controller/:action/:id/:status', array('module' => 'admin')));
43 $router->addRoute('admin_action_id', new Zend_Controller_Router_Route('admin/:controller/:action/:id', array('module' => 'admin')));
44 $router->addRoute('admin_action', new Zend_Controller_Router_Route('admin/:controller/:action', array('module' => 'admin')));
45 $router->addRoute('admin_controller', new Zend_Controller_Router_Route('admin/:controller', array('module' => 'admin')));
46 $router->addRoute('admin', new Zend_Controller_Router_Route('admin/', array('module' => 'admin')));
47
48 /**** here ****/
49 /*
50 $route = new Zend_Controller_Router_Route_Regex(
51 '([^-]*)-([^-]*)-([^-]*)-([^-]*)-([^-]*)\.html',
52 array(
53 'module' => 'default'
54 ),
55 array(
56 1 => 'controller',
57 2 => 'action',
58 3 => 'id',
59 4 => 'status',
60 5 => 'seo'
61 ));
62 $router->addRoute('default4', $route);
63
64 $route = new Zend_Controller_Router_Route_Regex(
65 '([^-]*)-([^-]*)-([^-]*)-([^-]*)\.html',
66 array(
67 'module' => 'default'
68 ),
69 array(
70 1 => 'controller',
71 2 => 'action',
72 3 => 'id',
73 4 => 'status'
74 ));
75 $router->addRoute('default4', $route);
76
77 $route = new Zend_Controller_Router_Route_Regex(
78 '([^-]*)-([^-]*)-([^-]*)\.html',
79 array(
80 'module' => 'default'
81 ),
82 array(
83 1 => 'controller',
84 2 => 'action',
85 3 => 'id',
86 ));
87 $router->addRoute('default3', $route);
88
89 $route = new Zend_Controller_Router_Route_Regex(
90 '([^-]*)-([^-]*)\.html',
91 array(
92 'module' => 'default'
93 ),
94 array(
95 1 => 'controller',
96 2 => 'action'
97 ));
98 $router->addRoute('default2', $route);
99
100
101 $route = new Zend_Controller_Router_Route_Regex(
102 '([^-]*)\.html',
103 array(
104 'module' => 'default',
105 'action' => 'index'
106 ),
107 array(
108 1 => 'controller'
109 ));
110 $router->addRoute('default', $route);
111
112 */
113
114 /*
115 $router->addRoute('default', new Zend_Controller_Router_Route(':action'), array('lang' => 'pl'));
116 */
117 //$router->removeDefaultRoutes();
118
119 /* error handler */
120 $errorHandler = new Zend_Controller_Plugin_ErrorHandler();
121 $errorHandler->setErrorHandlerModule('default')
122 ->setErrorHandlerController('error')
123 ->setErrorHandlerAction('error');
124
125
126 // setup controller
127 $frontController = Zend_Controller_Front::getInstance();
128 $frontController->throwExceptions(false);
129 $frontController->setParam('noViewRenderer', true);
130
131 $frontController->setRouter($router)
132 ->setControllerDirectory(array(
133 'default'=>'./application/default_controllers',
134 'admin'=>'./application/admin_controllers'))
135 // ->addModuleDirectory('./application/modules');
136 // ->setModuleControllerDirectoryName('controllers')
137 // ->setBaseUrl( $url )
138 ;
139
140
141 // run!
142 $frontController->dispatch();
143
144 $time_end = getmicrotime();
145
146 $time_exec = $time_end-$time_start;
147 $time_baza = $baza_end-$baza_start;
148 //echo '<p style="clear: both; float: right;color: #9ae;">wygenerowano w: '.$time_exec.' s.<br />polaczenie z baza: '.$time_baza.' s.</p>';
149
150 ?>