RoutingFilter.php 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. <?php
  2. /**
  3. * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
  4. * Copyright (c) Cake Software Foundation, Inc. (http://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. (http://cakefoundation.org)
  11. * @link http://cakephp.org CakePHP(tm) Project
  12. * @since 3.0.0
  13. * @license http://www.opensource.org/licenses/mit-license.php MIT License
  14. */
  15. namespace Cake\Routing\Filter;
  16. use Cake\Event\Event;
  17. use Cake\Routing\DispatcherFilter;
  18. use Cake\Routing\Exception\RedirectException;
  19. use Cake\Routing\Router;
  20. /**
  21. * A dispatcher filter that applies routing rules to the request.
  22. *
  23. * This filter will call Router::parse() when the request has no controller
  24. * parameter defined.
  25. */
  26. class RoutingFilter extends DispatcherFilter
  27. {
  28. /**
  29. * Priority setting.
  30. *
  31. * This filter is normally fired last just before the request
  32. * is dispatched.
  33. *
  34. * @var int
  35. */
  36. protected $_priority = 10;
  37. /**
  38. * Applies Routing and additionalParameters to the request to be dispatched.
  39. * If Routes have not been loaded they will be loaded, and config/routes.php will be run.
  40. *
  41. * @param \Cake\Event\Event $event containing the request, response and additional params
  42. * @return \Cake\Network\Response|null A response will be returned when a redirect route is encountered.
  43. */
  44. public function beforeDispatch(Event $event)
  45. {
  46. $request = $event->data('request');
  47. if (Router::getRequest(true) !== $request) {
  48. Router::setRequestInfo($request);
  49. }
  50. try {
  51. if (empty($request->params['controller'])) {
  52. $params = Router::parse($request->url, $request->method());
  53. $request->addParams($params);
  54. }
  55. return null;
  56. } catch (RedirectException $e) {
  57. $event->stopPropagation();
  58. $response = $event->data('response');
  59. $response->statusCode($e->getCode());
  60. $response->header('Location', $e->getMessage());
  61. return $response;
  62. }
  63. }
  64. }