fixed up router
This commit is contained in:
@@ -4,22 +4,30 @@ class Router {
|
||||
public $routes = [];
|
||||
public $query = [];
|
||||
public $path;
|
||||
public $controllerPath = BASEPATH . '/App/controllers/404.php';
|
||||
public $controller;
|
||||
public $controllerPath;
|
||||
public $parameters = [];
|
||||
|
||||
public function __construct() {
|
||||
$this->loadRoutes();
|
||||
$this->preparePath();
|
||||
$this->prepareQuery();
|
||||
$this->setRouteFile();
|
||||
$this->routes = $this->loadRoutes();
|
||||
$this->path = $this->preparePath();
|
||||
$this->query = $this->prepareQuery();
|
||||
$this->controller = $this->findController();
|
||||
$this->controllerPath = $this->setRouteFile();
|
||||
}
|
||||
|
||||
private function loadRoutes() {
|
||||
require_once( BASEPATH . '/App/routes.php');
|
||||
$this->routes = $routes;
|
||||
return $routes;
|
||||
}
|
||||
|
||||
private function preparePath() {
|
||||
$this->path = parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH);
|
||||
$path = parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH);
|
||||
$path = rtrim($path, '/'); // remove trailing slash
|
||||
//remove anything after and including ampersand
|
||||
$path = preg_replace('/&.+$/', '', $path);
|
||||
|
||||
return $path;
|
||||
}
|
||||
|
||||
private function prepareQuery() {
|
||||
@@ -28,21 +36,74 @@ class Router {
|
||||
if (isset($parsedUri['query'])) {
|
||||
parse_str($parsedUri['query'], $queryArray);
|
||||
}
|
||||
$this->query = $queryArray;
|
||||
return $queryArray;
|
||||
}
|
||||
|
||||
private function setRouteFile() {
|
||||
foreach ($this->routes as $key => $value) {
|
||||
if ( $this->path == $key) {
|
||||
$theFile = BASEPATH . '/App/controllers/' . $value['file'] . '.php';
|
||||
private function findController() {
|
||||
|
||||
//check if the file exists
|
||||
if (file_exists($theFile)) {
|
||||
$this->controllerPath = $theFile;
|
||||
// one to one match
|
||||
if (array_key_exists($this->path, $this->routes)) {
|
||||
return $this->routes[$this->path]['file'];
|
||||
}
|
||||
|
||||
foreach ($this->routes as $key => $value) {
|
||||
// Check if key contains a curly bracket, if not continue. We already checked above.
|
||||
if (!strpos($key, '{')) continue;
|
||||
|
||||
// Remove everything after the curly bracket, from key
|
||||
$keyPath = substr($key, 0, strpos($key, '{'));
|
||||
|
||||
//see if keyPath matches the first characters of $this->path, only the first characters have to match
|
||||
if (strpos($this->path, $keyPath) === 0) {
|
||||
|
||||
// We have a potential match. Now check if the parameter count is equal
|
||||
$keyParams = explode('/', $key);
|
||||
$pathParams = explode('/', $this->path);
|
||||
$keyParamCount = count($keyParams);
|
||||
$pathParamCount = count($pathParams);
|
||||
if ($keyParamCount === $pathParamCount) {
|
||||
for ($i=0; $i < $pathParamCount; $i++) {
|
||||
if (strpos($keyParams[$i], '{') !== false) {
|
||||
$keyParams[$i] = substr($keyParams[$i], 1, -1);
|
||||
$this->parameters[$keyParams[$i]] = $pathParams[$i];
|
||||
return $this->routes[$key]['file'];
|
||||
}
|
||||
}
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
return '404';
|
||||
|
||||
}
|
||||
|
||||
// checks if the file exists, sets file path
|
||||
private function setRouteFile() {
|
||||
|
||||
$cp = BASEPATH . '/App/controllers/' . $this->controller . '.php';
|
||||
|
||||
if (file_exists($cp)) {
|
||||
return $cp;
|
||||
} else {
|
||||
return BASEPATH . '/App/controllers/404.php';
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public function debug() {
|
||||
echo '<h1>Debugging Router</h1>';
|
||||
echo '<h2>Url Path</h2>';
|
||||
echo $this->path . '<br>';
|
||||
echo '<h2>ControllerPath</h2>';
|
||||
echo $this->controllerPath;
|
||||
echo '<h2>Parameters</h2>';
|
||||
echo '<pre>';
|
||||
print_r($this->parameters);
|
||||
echo '</pre>';
|
||||
echo '<h2>Routes Variable</h2><pre>';
|
||||
print_r($this->routes);
|
||||
echo '</pre>';
|
||||
die();
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user