From 52612b24581e5e8512c9b3b8057630767038102e Mon Sep 17 00:00:00 2001 From: habarnam Date: Sat, 28 Jun 2008 18:42:13 +0300 Subject: [PATCH] * simplifications of tsUrl and tsRouter - still not working fully --- _res/_libs/tsrouter.class.php | 29 ++++++++++++++--------------- _res/_libs/tsurl.class.php | 43 +++++++++++++++++++++++++------------------ 2 files changed, 39 insertions(+), 33 deletions(-) diff --git a/_res/_libs/tsrouter.class.php b/_res/_libs/tsrouter.class.php index 9dc7543..74e13da 100644 --- a/_res/_libs/tsrouter.class.php +++ b/_res/_libs/tsrouter.class.php @@ -8,6 +8,7 @@ class tsRouter { // adding default routes for css and rss $this->addRoute ('rss'); $this->addRoute ('style'); + $this->addRoute ('index'); } public function addRoute ($ctrlName, $incRoute = null, $default = null) { @@ -26,10 +27,12 @@ class tsRouter { } public function getController () { - $to = urldecode(tsUrl::getRequest('to')); - - if (empty($this->routes)) - return new tsControllerHtml(); + $to = urldecode(tsUrl::getRequest(NAV_VAR)); // due to bug(?) in lighttpd url rewrite + var_dump($to); + if (empty($this->routes)) { + if ($this->validController ($to)) + return new $to; + } foreach ($this->routes as $ctrlName => $regex) { // var_dump($to, $ctrlName, $regex,preg_match('/'.$ctrlName.'/i', $to), preg_match($regex, $to,$a)); @@ -44,17 +47,13 @@ class tsRouter { // die; } - private function validController ($ctrlName) { -// echo PAGE_PATH . $ctrlName . DIRECTORY_SEPARATOR . 'ctrl.php'; - if (!empty($ctrlName) && is_dir(PAGE_PATH . $ctrlName) && is_file(PAGE_PATH . $ctrlName . DIRECTORY_SEPARATOR . 'ctrl.php')) { - include (PAGE_PATH . $ctrlName . DIRECTORY_SEPARATOR . 'ctrl.php'); - }elseif (empty($ctrlName)) { - $ctrlName = 'index'; - include (PAGE_PATH . $ctrlName . DIRECTORY_SEPARATOR . 'ctrl.php'); - } else { - $ctrlName = 'tsControllerHtml'; - } + public function validController ($ctrlName) { + echo $ctrlName = strtolower($ctrlName); +// echo PAGE_PATH . $ctrlName . DIRECTORY_SEPARATOR . $ctrlName . '.php'; + if (!empty($ctrlName) && is_dir(PAGE_PATH . $ctrlName) && is_file (PAGE_PATH . $ctrlName . DIRECTORY_SEPARATOR . $ctrlName . '.php')) { + return true; + } - return new $ctrlName; + return false; } } diff --git a/_res/_libs/tsurl.class.php b/_res/_libs/tsurl.class.php index bdf24cd..2298476 100644 --- a/_res/_libs/tsurl.class.php +++ b/_res/_libs/tsurl.class.php @@ -70,39 +70,46 @@ class tsUrl { } if (stristr ($string,'/')) substr ($string, 0, -1); + // returns what's right of $glue=: in $string return str_replace ($varName . ':', '', $string); } + // we have no var name on the left of glue return $string; } static function getVarName ($string) { if (stristr ($string, ':')) { + // returns what's left of $glue=: in $string (ie, the var name) return str_replace (stristr ($string,':'), '', $string); } - return $string; + return null; } static private function getFriendlyParams () { - $sizeof = 1; - list($urlStr) = array_keys ($_GET); - $parArr = explode ('/', $urlStr); // fixme: $separator needed - - $retArr[NAV_VAR] = tsUrl::getValue ($parArr[0], NAV_VAR); - $retArr[ACT_VAR] = tsUrl::getValue ($parArr[1], ACT_VAR); - - // we have a 'do' action - if ($retArr[ACT_VAR]) - $sizeof = 2; + $parArr = explode ('/', $_SERVER['REQUEST_URI']); // fixme: $separator needed + array_shift($parArr); + $size = sizeof ($parArr); - if (sizeof ($parArr) > $sizeof) { - $others = array_slice ($parArr, $sizeof); - - foreach ($others as $cont) { - $key = tsUrl::getVarName ($cont); - $val = tsUrl::getValue ($cont, $key); - $retArr[$key] = $val; + while ($size > 0) { + foreach ($parArr as $cont) { + $key = tsUrl::getVarName ($cont); + $retArr[] = tsUrl::getValue ($cont, $key); + $size--; } } + + // navigation variable - logically first + if (!empty ($retArr[0])) { + $retArr[NAV_VAR] = $retArr[0]; + unset ($retArr[0]); + } + // action variable - right after + if (!empty ($retArr[1])) { + $retArr[ACT_VAR] = $retArr[1]; + unset ($retArr[1]); + } + +// var_dump($retArr); return $retArr; } -- 2.11.4.GIT