* simplifications of tsUrl and tsRouter - still not working fully
[vsc.git] / _res / _libs / tsrouter.class.php
blob74e13dae1a8b3926f1e5ccccd114b2cc38d521ac
1 <?php
3 class tsRouter {
4 protected $routes = array(),
5 $defaultRoute;
7 public function __construct (){
8 // adding default routes for css and rss
9 $this->addRoute ('rss');
10 $this->addRoute ('style');
11 $this->addRoute ('index');
14 public function addRoute ($ctrlName, $incRoute = null, $default = null) {
15 if (empty ($ctrlName)) {
16 return false;
19 if (empty ($incRoute))
20 $incRoute = '/'.$ctrlName.'/';
22 if ($default)
23 $this->defaultRoute = $ctrlName;
25 $this->routes[$ctrlName] = $incRoute;
26 return true;
29 public function getController () {
30 $to = urldecode(tsUrl::getRequest(NAV_VAR)); // due to bug(?) in lighttpd url rewrite
31 var_dump($to);
32 if (empty($this->routes)) {
33 if ($this->validController ($to))
34 return new $to;
37 foreach ($this->routes as $ctrlName => $regex) {
38 // var_dump($to, $ctrlName, $regex,preg_match('/'.$ctrlName.'/i', $to), preg_match($regex, $to,$a));
39 // if (!empty($a))
40 // var_dump($a);
41 // echo "<br/>";
42 if ( preg_match('/'.$ctrlName.'/i', $to) || preg_match($regex, $to)) {
43 return $this->validController ($ctrlName);
46 return $this->validController($this->defaultRoute);
47 // die;
50 public function validController ($ctrlName) {
51 echo $ctrlName = strtolower($ctrlName);
52 // echo PAGE_PATH . $ctrlName . DIRECTORY_SEPARATOR . $ctrlName . '.php';
53 if (!empty($ctrlName) && is_dir(PAGE_PATH . $ctrlName) && is_file (PAGE_PATH . $ctrlName . DIRECTORY_SEPARATOR . $ctrlName . '.php')) {
54 return true;
57 return false;