2 /** @package verysimple::Phreeze */
3 require_once("verysimple/HTTP/RequestUtil.php");
4 require_once("verysimple/Util/UrlWriterMode.php");
5 require_once("verysimple/Phreeze/IRouter.php");
8 * class for dealing with URLs
10 * @package verysimple::HTTP
11 * @author VerySimple Inc.
12 * @copyright 1997-2007 VerySimple, Inc. http://www.verysimple.com
13 * @license http://www.gnu.org/licenses/lgpl.html LGPL
16 class ActionRouter
implements IRouter
20 private $_defaultRoute;
21 protected $stripApi = true;
22 protected $delim = '&';
23 protected static $_format;
26 * Constructor allows a rewriting pattern to be specified
28 * @param string $format
29 * sprintf compatible format
31 public function __construct($format = "%s.%s.page?%s", $mode = UrlWriterMode
::WEB
, $appRoot = '', $defaultRoute = '')
33 self
::$_format = $format;
35 $this->_appRoot
= $appRoot;
36 $this->_defaultRoute
= $defaultRoute;
42 public function GetUri()
44 return implode('/', RequestUtil
::GetUrlParts($this->_appRoot
));
50 public function GetUrlParams()
58 public function GetUrlParam($key, $default = '')
60 // make the route params case insensitive
61 return RequestUtil
::Get($key, $default, false, true);
67 public function GetUrl($controller, $method, $params = '', $requestMethod = '')
69 $format = str_replace("{delim}", $this->delim
, self
::$_format);
73 if (is_array($params)) {
74 foreach ($params as $key => $val) {
75 // if no val, the omit the equal sign (this might be used in rest-type requests)
76 $qs .= $d . $key . (strlen($val) ?
("=" . urlencode($val)) : "");
83 $url = sprintf($format, $controller, $method, $qs);
85 // strip off trailing delimiters from the url
86 $url = (substr($url, - 5) == "&") ?
substr($url, 0, strlen($url) - 5) : $url;
87 $url = (substr($url, - 1) == "&" ||
substr($url, - 1) == "?") ?
substr($url, 0, strlen($url) - 1) : $url;
89 $api_check = explode("/api/", RequestUtil
::GetCurrentUrl());
90 if ($this->stripApi
&& count($api_check) > 1) {
91 $url = $api_check [0] . "/" . $url;
100 public function GetRoute($uri = "")
103 $action = RequestUtil
::Get('action');
105 $action = $this->_defaultRoute
;
108 $uri = $action ?
$action : RequestUtil
::GetCurrentURL();
111 // get the action requested
112 $params = explode(".", str_replace("/", ".", $uri));
113 $controller_param = isset($params [0]) && $params [0] ?
$params [0] : "";
114 $controller_param = str_replace(array (
122 ), $controller_param);
124 if (! $controller_param) {
125 throw new Exception("Invalid or missing Controller parameter");
128 $method_param = isset($params [1]) && $params [1] ?
$params [1] : "";
129 if (! $method_param) {
130 $method_param = "DefaultAction";
140 * Returns true or false based on the $value passed in as to whether or not the
141 * URL Writer is currently in that mode.
143 * @param $value String
144 * mode to check against the current mode
145 * @return boolean TRUE if arg passed in is the current mode
147 public function ModeIs($value)
149 if (strcmp($this->_mode
, $value) == 0) {
157 * Returns how the Dispatcher plucks it's controller and method from the URL.
159 * @param $default_action The
160 * Default action in case the argument hasn't been supplied
162 public function GetAction($url_param = "action", $default_action = "Account.DefaultAction")
164 switch ($this->_mode
) {
165 // TODO: Determine mobile/joomla URL action (if different from default)
167 * case UrlWriterMode::JOOMLA:
169 * case UrlWriterMode::MOBILE:
173 // default is to return the standard browser-based action=%s.%s&%s:
174 return RequestUtil
::Get($url_param, $default_action);