mirror of
https://github.com/skipperbent/simple-php-router.git
synced 2026-06-17 00:37:52 +00:00
[TASK] Bugfixes and improvements
- Most routes now works along with getRoute() method.
This commit is contained in:
@@ -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);
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user