first commit
This commit is contained in:
43
src/Router.php
Normal file
43
src/Router.php
Normal file
@@ -0,0 +1,43 @@
|
||||
<?php
|
||||
|
||||
class Router {
|
||||
public $routes = [];
|
||||
public $query = [];
|
||||
public $path;
|
||||
public $controllerPath = BASEPATH . '/App/controllers/404.php';
|
||||
|
||||
public function __construct() {
|
||||
$this->loadRoutes();
|
||||
$this->preparePath();
|
||||
$this->prepareQuery();
|
||||
$this->setRouteFile();
|
||||
}
|
||||
|
||||
private function loadRoutes() {
|
||||
require_once( BASEPATH . '/App/routes.php');
|
||||
$this->routes = $routes;
|
||||
}
|
||||
|
||||
private function preparePath() {
|
||||
$this->path = parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH);
|
||||
}
|
||||
|
||||
private function prepareQuery() {
|
||||
$parsedUri = parse_url($_SERVER['REQUEST_URI']);
|
||||
$queryArray = [];
|
||||
if (isset($parsedUri['query'])) {
|
||||
parse_str($parsedUri['query'], $queryArray);
|
||||
}
|
||||
$this->query = $queryArray;
|
||||
}
|
||||
|
||||
private function setRouteFile() {
|
||||
foreach ($this->routes as $key => $value) {
|
||||
if ( $this->path == $key) {
|
||||
$this->controllerPath = BASEPATH . '/App/controllers/' . $value['file'] . '.php';
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user