fixed up router
This commit is contained in:
		
							parent
							
								
									713a44ad98
								
							
						
					
					
						commit
						0c96ba6c90
					
				
							
								
								
									
										75
									
								
								src/Database.php
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										75
									
								
								src/Database.php
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,75 @@
 | 
			
		||||
<?php
 | 
			
		||||
 | 
			
		||||
class Database {
 | 
			
		||||
 | 
			
		||||
    private $host;
 | 
			
		||||
    private $user;
 | 
			
		||||
    private $pass;
 | 
			
		||||
    private $dbname;
 | 
			
		||||
    private $conn;
 | 
			
		||||
 | 
			
		||||
    public function __construct($dbinfo) {
 | 
			
		||||
        $this->host = $dbinfo['host'];
 | 
			
		||||
        $this->user = $dbinfo['user'];
 | 
			
		||||
        $this->pass = $dbinfo['pass'];
 | 
			
		||||
        $this->dbname = $dbinfo['name'];
 | 
			
		||||
 | 
			
		||||
        $this->connect();
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    private function connect() {
 | 
			
		||||
        $this->conn = new mysqli($this->host, $this->user, $this->pass, $this->dbname);
 | 
			
		||||
 | 
			
		||||
        if ($this->conn->connect_error) {
 | 
			
		||||
            die("Connection failed: " . $this->conn->connect_error);
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public function query($query) {
 | 
			
		||||
        $stmt = $this->conn->prepare($query);
 | 
			
		||||
        $stmt->execute();
 | 
			
		||||
 | 
			
		||||
        return $stmt->get_result();
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public function getRow($query) {
 | 
			
		||||
        $result = $this->query($query);
 | 
			
		||||
        return $result->fetch_assoc();
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public function debugGetRow($query) {
 | 
			
		||||
        echo "<h1>Debug GetRow Query</h1>";
 | 
			
		||||
        echo "<div class='debug-query'>Query: $query</div>";
 | 
			
		||||
        $result = $this->query($query);
 | 
			
		||||
        $row = $result->fetch_assoc();
 | 
			
		||||
    
 | 
			
		||||
        echo "<pre>";
 | 
			
		||||
        print_r($row);
 | 
			
		||||
        echo "</pre>";
 | 
			
		||||
    
 | 
			
		||||
        die();
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public function getRows($query) {
 | 
			
		||||
        $result = $this->query($query);
 | 
			
		||||
        return $result->fetch_all(MYSQLI_ASSOC);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public function debugGetRows($query) {
 | 
			
		||||
        echo "<h1>Debug GetRows Query</h1>";
 | 
			
		||||
        echo "<div class='debug-query'>Query: $query</div>";
 | 
			
		||||
    
 | 
			
		||||
        $result = $this->query($query);
 | 
			
		||||
        $rows = $result->fetch_all(MYSQLI_ASSOC);
 | 
			
		||||
    
 | 
			
		||||
        echo "<pre>";
 | 
			
		||||
        print_r($rows);
 | 
			
		||||
        echo "</pre>";
 | 
			
		||||
    
 | 
			
		||||
        die();
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public function close() {
 | 
			
		||||
        $this->conn->close();
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
@ -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 findController() {
 | 
			
		||||
 | 
			
		||||
        // one to one match
 | 
			
		||||
        if (array_key_exists($this->path, $this->routes)) {
 | 
			
		||||
            return $this->routes[$this->path]['file'];
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
    private function setRouteFile() {
 | 
			
		||||
        foreach ($this->routes as $key => $value) {
 | 
			
		||||
            if ( $this->path == $key) {
 | 
			
		||||
                $theFile = BASEPATH . '/App/controllers/' . $value['file'] . '.php';
 | 
			
		||||
            // Check if key contains a curly bracket, if not continue.  We already checked above.
 | 
			
		||||
            if (!strpos($key, '{')) continue;
 | 
			
		||||
 | 
			
		||||
                //check if the file exists
 | 
			
		||||
                if (file_exists($theFile)) {
 | 
			
		||||
                    $this->controllerPath = $theFile;
 | 
			
		||||
                }
 | 
			
		||||
                break;
 | 
			
		||||
            // 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'];
 | 
			
		||||
                        }
 | 
			
		||||
                    }
 | 
			
		||||
                }
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
        
 | 
			
		||||
        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();
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
@ -1,5 +1,6 @@
 | 
			
		||||
<?php
 | 
			
		||||
require_once(BASEPATH . '/vendor/autoload.php');
 | 
			
		||||
require_once(BASEPATH . '/App/config.php');
 | 
			
		||||
 | 
			
		||||
//Twig
 | 
			
		||||
function view($name = '', $data = [] ) {
 | 
			
		||||
@ -17,7 +18,12 @@ function view($name = '', $data = [] ) {
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// Load Database Class
 | 
			
		||||
require_once(BASEPATH . '/vendor/4lt/novaconium/src/Database.php');
 | 
			
		||||
$db = new Database($config['database']);
 | 
			
		||||
 | 
			
		||||
// Load a controller
 | 
			
		||||
require_once('Router.php');
 | 
			
		||||
require_once(BASEPATH . '/vendor/4lt/novaconium/src/Router.php');
 | 
			
		||||
$router = new Router();
 | 
			
		||||
//$router->debug();
 | 
			
		||||
require_once($router->controllerPath);
 | 
			
		||||
 | 
			
		||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user