/nx/lib/Router.php

https://github.com/noyse1987/nx · PHP · 108 lines · 53 code · 14 blank · 41 comment · 4 complexity · c8f2dbb3d731c3a1cb6768c0dad91a66 MD5 · raw file

  1. <?php
  2. /**
  3. * NX
  4. *
  5. * @author Nick Sinopoli <NSinopoli@gmail.com>
  6. * @copyright Copyright (c) 2011, Nick Sinopoli
  7. * @license http://opensource.org/licenses/bsd-license.php The BSD License
  8. */
  9. namespace nx\lib;
  10. /*
  11. * The `Router` class is used to handle url routing.
  12. *
  13. * @package lib
  14. */
  15. class Router {
  16. /**
  17. * The routing defaults.
  18. *
  19. * @var array
  20. * @access protected
  21. */
  22. public static $defaults = array(
  23. 'controller' => 'Dashboard',
  24. 'action' => 'index',
  25. 'id' => null
  26. );
  27. /**
  28. * The url rewrite scheme.
  29. *
  30. * @var array
  31. * @access protected
  32. */
  33. protected static $_routes = array(
  34. '/^\/?$/' => '', // this will return our defaults
  35. '/^\/([A-Za-z0-9\-]+)\/?$/' => 'controller=$1',
  36. '/^\/([A-Za-z0-9\-]+)\?(.+)$/' => 'controller=$1&args=$2',
  37. '/^\/([A-Za-z0-9\-]+)\/([\d]+)\/?$/' => 'controller=$1&id=$2',
  38. '/^\/([A-Za-z0-9\-]+)\/([\d]+)\?(.+)$/' => 'controller=$1&id=$2&args=$3',
  39. '/^\/([A-Za-z0-9\-]+)\/([A-Za-z0-9\-_]+)\/?$/' => 'controller=$1&action=$2',
  40. '/^\/([A-Za-z0-9\-]+)\/([A-Za-z0-9\-_]+)\?(.+)$/' => 'controller=$1&action=$2&args=$3',
  41. '/^\/([A-Za-z0-9\-]+)\/([A-Za-z0-9\-_]+)\/([\d]+)\/?$/' => 'controller=$1&action=$2&id=$3',
  42. '/^\/([A-Za-z0-9\-]+)\/([A-Za-z0-9\-_]+)\/([\d]+)\?(.+)$/' => 'controller=$1&action=$2&id=$3&args=$4'
  43. );
  44. /**
  45. * Parses a query string and returns the controller, action,
  46. * id, and any additional arguments passed via $_GET.
  47. *
  48. * @param string $query_string The controller name.
  49. * @access public
  50. * @return array
  51. */
  52. protected static function _parse_query_string($query_string) {
  53. $query = array();
  54. parse_str($query_string, $query);
  55. $controller = ( isset($query['controller']) )
  56. ? ucfirst($query['controller'])
  57. : self::$defaults['controller'];
  58. $action = ( isset($query['action']) )
  59. ? $query['action']
  60. : self::$defaults['action'];
  61. $id = ( isset($query['id']) )
  62. ? $query['id']
  63. : self::$defaults['id'];
  64. $get = array();
  65. if ( isset($query['args']) && $query['args'] != '' ) {
  66. $args = substr($query_string, strpos($query_string, $query['args']));
  67. parse_str($args, $get);
  68. }
  69. return compact('controller', 'action', 'id', 'get');
  70. }
  71. /**
  72. * Parses a url in accordance with the defined routes,
  73. * and returns the necessary components to route the request.
  74. *
  75. * @see nx\lib\Router::_parse_query_string()
  76. * @param string $url The url.
  77. * @access public
  78. * @return array
  79. */
  80. public static function parse_url($url) {
  81. $matches = array();
  82. foreach ( self::$_routes as $pattern => $route ) {
  83. if ( preg_match($pattern, $url, $matches) ) {
  84. unset($matches[0]);
  85. foreach ( $matches as $key => $match ) {
  86. $route = str_replace('$' . $key, $match, $route);
  87. }
  88. return self::_parse_query_string($route);
  89. }
  90. }
  91. return false;
  92. }
  93. }
  94. ?>