2 /** @package verysimple::Phreeze */
3 require_once ("verysimple/HTTP/RequestUtil.php");
4 require_once ("verysimple/Phreeze/IRouter.php");
7 * SimpleRouter is a URL router that parses URLs in the following format:
8 * http://server/index.php?ROUTE
9 * This router can be used in a situation where URL re-writing is not
10 * available or wanted on the host server
12 * The route must be the first param in the querysting and does not
13 * have a key. Additional querystring parameters can be appended
14 * and will have no effect on the route.
16 * Note that this router does not currently support different routes
17 * based on params passed through nor is wildcard matching supported
19 * @package verysimple::HTTP
20 * @author VerySimple Inc.
21 * @copyright 1997-2007 VerySimple, Inc. http://www.verysimple.com
22 * @license http://www.gnu.org/licenses/lgpl.html LGPL
25 class SimpleRouter
implements IRouter
{
26 public static $ROUTE_NOT_FOUND = "Default.Error404";
27 private $routes = array ();
28 private $defaultAction = 'Default.Home';
29 private $appRootUrl = '';
33 * @param string $appRootUrl
34 * @param string $defaultAction
35 * @param array $mapping
38 public function __construct($appRootUrl = '', $defaultAction = '', $mapping = array()) {
40 $this->defaultAction
= $defaultAction;
41 $this->routes
= $mapping;
45 * Given a controller, method and params, returns a url that points
46 * to the correct location
48 * @param string $controller
49 * @param string $method
50 * @param array $params
51 * in the format param1=val1¶m2=val2
54 public function GetUrl($controller, $method, $params = '', $requestMethod = '') {
55 throw new Exception ( 'Not yet implemented' );
59 * Returns the controller and method for the given URI
62 * string the url, if not provided will be obtained using the current URL
63 * @return array($controller,$method)
65 public function GetRoute($uri = "") {
68 $qs = $uri ?
$uri : (array_key_exists ( 'QUERY_STRING', $_SERVER ) ?
$_SERVER ['QUERY_STRING'] : '');
69 $parsed = explode ( '&', $qs, 2 );
70 $action = $parsed [0];
72 if (strpos ( $action, '=' ) > - 1 ||
! $action) {
73 // the querystring is empty or the first param is a named param, which we ignore
74 $match = $this->defaultAction
;
76 // otherwise we have a route. see if we have a match in the routemap, otherwise return the 'not found' route
77 $method = RequestUtil
::GetMethod ();
78 $route = $method . ':' . $action;
79 $match = array_key_exists ( $route, $this->routes
) ?
$this->routes
[$route] ['route'] : self
::$ROUTE_NOT_FOUND;
82 return explode ( '.', $match, 2 );
86 * In the case of a rewrite url, the url itself contains the parameter
87 * for example http://server/param1/param2/param3.
89 * are parsed and the param with the given index is returned
91 * @return string (or $default if not provided)
93 * string default value to return if parameter is empty
95 public function GetUrlParam($key, $default = '') {
96 return array_key_exists ( $key, $_REQUEST ) ?
$_REQUEST [$key] : $default;
100 * In the case of a rewrite url, the url itself contains the parameter
101 * for example http://server/param1/param2/param3.
103 * are parsed and returned as an array
107 public function GetUrlParams() {
112 * Returns the RESTful part of the url
113 * For example, localhost/users/5 will return users/5
117 public function GetUri() {