Dispatcher.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. <?php
  2. /**
  3. * CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
  4. * Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
  5. *
  6. * Licensed under The MIT License
  7. * For full copyright and license information, please see the LICENSE.txt
  8. * Redistributions of files must retain the above copyright notice.
  9. *
  10. * @copyright Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
  11. * @link https://cakephp.org CakePHP(tm) Project
  12. * @since 0.2.9
  13. * @license https://opensource.org/licenses/mit-license.php MIT License
  14. */
  15. namespace Cake\Routing;
  16. use Cake\Event\EventDispatcherTrait;
  17. use Cake\Event\EventListenerInterface;
  18. use Cake\Http\ActionDispatcher;
  19. use Cake\Http\Response;
  20. use Cake\Http\ServerRequest;
  21. /**
  22. * Dispatcher converts Requests into controller actions. It uses the dispatched Request
  23. * to locate and load the correct controller. If found, the requested action is called on
  24. * the controller
  25. *
  26. * @deprecated 3.6.0 Dispatcher is deprecated. You should update your application to use
  27. * the Http\Server implementation instead.
  28. */
  29. class Dispatcher
  30. {
  31. use EventDispatcherTrait;
  32. /**
  33. * Connected filter objects
  34. *
  35. * @var \Cake\Event\EventListenerInterface[]
  36. */
  37. protected $_filters = [];
  38. /**
  39. * Dispatches and invokes given Request, handing over control to the involved controller. If the controller is set
  40. * to autoRender, via Controller::$autoRender, then Dispatcher will render the view.
  41. *
  42. * Actions in CakePHP can be any public method on a controller, that is not declared in Controller. If you
  43. * want controller methods to be public and in-accessible by URL, then prefix them with a `_`.
  44. * For example `public function _loadPosts() { }` would not be accessible via URL. Private and protected methods
  45. * are also not accessible via URL.
  46. *
  47. * If no controller of given name can be found, invoke() will throw an exception.
  48. * If the controller is found, and the action is not found an exception will be thrown.
  49. *
  50. * @param \Cake\Http\ServerRequest $request Request object to dispatch.
  51. * @param \Cake\Http\Response $response Response object to put the results of the dispatch into.
  52. * @return string|null if `$request['return']` is set then it returns response body, null otherwise
  53. * @throws \LogicException When the controller did not get created in the Dispatcher.beforeDispatch event.
  54. */
  55. public function dispatch(ServerRequest $request, Response $response)
  56. {
  57. deprecationWarning(
  58. 'Dispatcher is deprecated. You should update your application to use ' .
  59. 'the Http\Server implementation instead.'
  60. );
  61. $actionDispatcher = new ActionDispatcher(null, $this->getEventManager(), $this->_filters);
  62. $response = $actionDispatcher->dispatch($request, $response);
  63. if ($request->getParam('return', null) !== null) {
  64. return $response->body();
  65. }
  66. return $response->send();
  67. }
  68. /**
  69. * Add a filter to this dispatcher.
  70. *
  71. * The added filter will be attached to the event manager used
  72. * by this dispatcher.
  73. *
  74. * @param \Cake\Event\EventListenerInterface $filter The filter to connect. Can be
  75. * any EventListenerInterface. Typically an instance of \Cake\Routing\DispatcherFilter.
  76. * @return void
  77. */
  78. public function addFilter(EventListenerInterface $filter)
  79. {
  80. $this->_filters[] = $filter;
  81. }
  82. /**
  83. * Get the list of connected filters.
  84. *
  85. * @return \Cake\Event\EventListenerInterface[]
  86. */
  87. public function filters()
  88. {
  89. return $this->_filters;
  90. }
  91. }