PluginShortRoute.php 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. <?php
  2. /**
  3. * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
  4. * Copyright 2005-2012, Cake Software Foundation, Inc. (http://cakefoundation.org)
  5. *
  6. * Licensed under The MIT License
  7. * Redistributions of files must retain the above copyright notice.
  8. *
  9. * @copyright Copyright 2005-2012, Cake Software Foundation, Inc. (http://cakefoundation.org)
  10. * @link http://cakephp.org CakePHP(tm) Project
  11. * @since CakePHP(tm) v 1.3
  12. * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
  13. */
  14. App::uses('CakeRoute', 'Routing/Route');
  15. /**
  16. * Plugin short route, that copies the plugin param to the controller parameters
  17. * It is used for supporting /:plugin routes.
  18. *
  19. * @package Cake.Routing.Route
  20. */
  21. class PluginShortRoute extends CakeRoute {
  22. /**
  23. * Parses a string url into an array. If a plugin key is found, it will be copied to the
  24. * controller parameter
  25. *
  26. * @param string $url The url to parse
  27. * @return mixed false on failure, or an array of request parameters
  28. */
  29. public function parse($url) {
  30. $params = parent::parse($url);
  31. if (!$params) {
  32. return false;
  33. }
  34. $params['controller'] = $params['plugin'];
  35. return $params;
  36. }
  37. /**
  38. * Reverse route plugin shortcut urls. If the plugin and controller
  39. * are not the same the match is an auto fail.
  40. *
  41. * @param array $url Array of parameters to convert to a string.
  42. * @return mixed either false or a string url.
  43. */
  44. public function match($url) {
  45. if (isset($url['controller']) && isset($url['plugin']) && $url['plugin'] != $url['controller']) {
  46. return false;
  47. }
  48. $this->defaults['controller'] = $url['controller'];
  49. $result = parent::match($url);
  50. unset($this->defaults['controller']);
  51. return $result;
  52. }
  53. }