PluginShortRoute.php 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  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 1.3.0
  13. * @license http://www.opensource.org/licenses/mit-license.php MIT License
  14. */
  15. namespace Cake\Routing\Route;
  16. use Cake\Routing\Route\Route;
  17. /**
  18. * Plugin short route, that copies the plugin param to the controller parameters
  19. * It is used for supporting /:plugin routes.
  20. *
  21. */
  22. class PluginShortRoute extends Route {
  23. /**
  24. * Parses a string URL into an array. If a plugin key is found, it will be copied to the
  25. * controller parameter
  26. *
  27. * @param string $url The URL to parse
  28. * @return mixed false on failure, or an array of request parameters
  29. */
  30. public function parse($url) {
  31. $params = parent::parse($url);
  32. if (!$params) {
  33. return false;
  34. }
  35. $params['controller'] = $params['plugin'];
  36. return $params;
  37. }
  38. /**
  39. * Reverse route plugin shortcut URLs. If the plugin and controller
  40. * are not the same the match is an auto fail.
  41. *
  42. * @param array $url Array of parameters to convert to a string.
  43. * @param array $context An array of the current request context.
  44. * Contains information such as the current host, scheme, port, and base
  45. * directory.
  46. * @return mixed either false or a string URL.
  47. */
  48. public function match(array $url, array $context = array()) {
  49. if (isset($url['controller']) && isset($url['plugin']) && $url['plugin'] !== $url['controller']) {
  50. return false;
  51. }
  52. $this->defaults['controller'] = $url['controller'];
  53. $result = parent::match($url, $context);
  54. unset($this->defaults['controller']);
  55. return $result;
  56. }
  57. }