[TASK] Bugfixes and improvements

- Most routes now works along with getRoute() method.
This commit is contained in:
Simon Sessingø
2015-10-06 16:08:42 +02:00
parent 1c765476b5
commit 6354c3c766
6 changed files with 160 additions and 132 deletions
+41 -1
View File
@@ -25,6 +25,14 @@ abstract class RouterEntry {
$this->parameters = array();
}
protected function loadClass($name) {
if(!class_exists($name)) {
throw new RouterException(sprintf('Class %s does not exist', $name));
}
return new $name();
}
/**
* @param string $callback
* @return self;
@@ -160,6 +168,38 @@ abstract class RouterEntry {
$this->settings[$name] = $value;
}
abstract function getRoute($requestMethod, &$url);
public function renderRoute($requestMethod) {
// Load middleware
if($this->getMiddleware()) {
$this->loadClass($this->getMiddleware());
}
if(is_object($this->getCallback()) && is_callable($this->getCallback())) {
// When the callback is a function
call_user_func_array($this->getCallback(), $this->getParameters());
} else {
// When the callback is a method
$controller = explode('@', $this->getCallback());
$className = $this->getNamespace() . '\\' . $controller[0];
$class = $this->loadClass($className);
$method = $requestMethod . ucfirst($controller[1]);
if (!method_exists($class, $method)) {
throw new RouterException(sprintf('Method %s does not exist in class %s', $method, $className), 404);
}
call_user_func_array(array($class, $method), $this->getParameters());
return $class;
}
return null;
}
abstract function matchRoute($requestMethod, $url);
}