Route.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. <?php
  2. /**
  3. * Lithium: the most rad php framework
  4. *
  5. * @copyright Copyright 2013, Union of RAD (http://union-of-rad.org)
  6. * @license http://opensource.org/licenses/bsd-license.php The BSD License
  7. */
  8. namespace lithium\console\command;
  9. use lithium\core\Libraries;
  10. use lithium\action\Request;
  11. use lithium\net\http\Router;
  12. use lithium\core\Environment;
  13. /**
  14. * The route command lets you inspect your routes and issue requests against the router.
  15. */
  16. class Route extends \lithium\console\Command {
  17. /**
  18. * Override the default 'development' environment.
  19. *
  20. * For example:
  21. * {{{
  22. * li3 route --env=production
  23. * li3 route show /foo --env=test
  24. * }}}
  25. *
  26. * @var string
  27. */
  28. public $env = 'development';
  29. /**
  30. * Load the routes file and set the environment.
  31. *
  32. * @param array $config The default configuration, wherein the absolute path to the routes file
  33. * to load may be specified, using the `'routes'` key.
  34. */
  35. public function __construct($config = array()) {
  36. $defaults = array('routes' => Libraries::get(true, 'path') . '/config/routes.php');
  37. parent::__construct($config + $defaults);
  38. }
  39. protected function _init() {
  40. parent::_init();
  41. Environment::set($this->env);
  42. if (file_exists($this->_config['routes'])) {
  43. return require $this->_config['routes'];
  44. }
  45. $this->error("The routes file for this library doesn't exist or can't be found.");
  46. }
  47. /**
  48. * Lists all connected routes to the router. See the `all()`
  49. * method for details and examples.
  50. *
  51. * @return void
  52. */
  53. public function run() {
  54. $this->all();
  55. }
  56. /**
  57. * Lists all connected routes to the router. This is a convenience
  58. * alias for the `show()` method.
  59. *
  60. * Example:
  61. * {{{
  62. * li3 route
  63. * li3 route all
  64. * }}}
  65. *
  66. * Will return an output similar to:
  67. *
  68. * {{{
  69. * Template Params
  70. * -------- ------
  71. * / {"controller":"pages","action":"view"}
  72. * /pages/{:args} {"controller":"pages","action":"view"}
  73. * /{:slug:[\w\-]+} {"controller":"posts","action":"show"}
  74. * /{:controller}/{:action}/{:args} {"action":"index"}
  75. * }}}
  76. *
  77. * @return void
  78. */
  79. public function all() {
  80. $routes = Router::get();
  81. $columns = array(array('Template', 'Params'), array('--------', '------'));
  82. foreach ($routes As $route) {
  83. $info = $route->export();
  84. $columns[] = array($info['template'], json_encode($info['params']));
  85. }
  86. $this->columns($columns);
  87. }
  88. /**
  89. * Returns the corresponding params for a given URL and an optional request
  90. * method.
  91. *
  92. * Examples:
  93. * {{{
  94. * 1: li3 route show /foo
  95. * 2: li3 route show post /foo/bar/1
  96. * 3: li3 route show /test
  97. * 4: li3 route show /test --env=production
  98. * }}}
  99. *
  100. * Will return outputs similar to:
  101. *
  102. * {{{
  103. * 1: {"controller":"foo","action":"index" }
  104. * 2: {"controller":"foo","action":"bar","args":["1"]}
  105. * 3: {"controller":"lithium\\test\\Controller","action":"index"}
  106. * 4: {"controller":"test","action":"index"}
  107. * }}}
  108. *
  109. * @return void
  110. */
  111. public function show() {
  112. $url = join(" ", $this->request->params['args']);
  113. $method = 'GET';
  114. if (!$url) {
  115. $this->error('Please provide a valid URL');
  116. }
  117. if (preg_match('/^(GET|POST|PUT|DELETE|HEAD|OPTIONS) (.+)/i', $url, $matches)) {
  118. $method = strtoupper($matches[1]);
  119. $url = $matches[2];
  120. }
  121. $request = new Request(compact('url') + array('env' => array('REQUEST_METHOD' => $method)));
  122. $result = Router::process($request);
  123. $this->out($result->params ? json_encode($result->params) : "No route found.");
  124. }
  125. }
  126. ?>