Front.php 28 KB

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