Compare commits
No commits in common. "master" and "1.0.1" have entirely different histories.
@ -16,7 +16,8 @@ Master Repo: https://git.4lt.ca/4lt/novaconium
|
|||||||
```bash
|
```bash
|
||||||
mkdir project_name;
|
mkdir project_name;
|
||||||
cd project_name;
|
cd project_name;
|
||||||
|
mkdir -p App/controllers App/templates App/views public
|
||||||
|
touch App/controllers/404.php App/controllers/index.php App/views/index.html.twig public/index.php public/.htaccess App/routes.php
|
||||||
|
|
||||||
composer require 4lt/novaconium
|
composer require 4lt/novaconium
|
||||||
cp -R vendor/4lt/novaconium/examples/App/ .
|
|
||||||
cp -R vendor/4lt/novaconium/examples/public/ .
|
|
||||||
```
|
```
|
||||||
|
@ -1,14 +1 @@
|
|||||||
<?php
|
<h1>This is 404</h1>
|
||||||
|
|
||||||
// Define our status code and message
|
|
||||||
$status_code = 404;
|
|
||||||
$status_message = 'The requested resource could not be found.';
|
|
||||||
|
|
||||||
// Set the HTTP response code and message
|
|
||||||
http_response_code($status_code);
|
|
||||||
header("Content-Type: text/html");
|
|
||||||
?>
|
|
||||||
|
|
||||||
<h1>Error 404 Resource Not found</h1>
|
|
||||||
<p><?php echo $status_message; ?></p>
|
|
||||||
|
|
@ -1,4 +1,6 @@
|
|||||||
RewriteEngine On
|
RewriteEngine On
|
||||||
|
|
||||||
RewriteCond %{REQUEST_FILENAME} !-f
|
RewriteCond %{REQUEST_FILENAME} !-f
|
||||||
RewriteCond %{REQUEST_FILENAME} !-d
|
RewriteCond %{REQUEST_FILENAME} !-d
|
||||||
|
|
||||||
RewriteRule ^(.*)$ index.php?_uri=$1 [QSA,L]
|
RewriteRule ^(.*)$ index.php?_uri=$1 [QSA,L]
|
6
examples/App/public/index.php
Normal file
6
examples/App/public/index.php
Normal file
@ -0,0 +1,6 @@
|
|||||||
|
<?php
|
||||||
|
error_reporting(E_ALL);
|
||||||
|
ini_set('display_errors', 1);
|
||||||
|
define('BASEPATH', dirname(__DIR__, 1));
|
||||||
|
require_once(BASEPATH . '/vendor/4lt/novaconium/src/bootstrap.php');
|
||||||
|
?>
|
@ -1,9 +1,9 @@
|
|||||||
<?php
|
<?php
|
||||||
$routes = [
|
$routes = [
|
||||||
'/about' => [
|
'/about' => [
|
||||||
'get' => 'about'
|
'file' => 'about'
|
||||||
],
|
],
|
||||||
'/' => [
|
'/' => [
|
||||||
'get' => 'index'
|
'file' => 'index'
|
||||||
]
|
]
|
||||||
];
|
];
|
@ -1 +0,0 @@
|
|||||||
{# Overrides go here #}
|
|
@ -1,6 +0,0 @@
|
|||||||
<?php
|
|
||||||
// error_reporting(E_ALL);
|
|
||||||
// ini_set('display_errors', 1);
|
|
||||||
define('BASEPATH', dirname(__DIR__, 1));
|
|
||||||
require_once(BASEPATH . '/vendor/4lt/novaconium/src/novaconium.php');
|
|
||||||
?>
|
|
@ -1,75 +0,0 @@
|
|||||||
<?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();
|
|
||||||
}
|
|
||||||
}
|
|
113
src/Router.php
113
src/Router.php
@ -4,40 +4,22 @@ class Router {
|
|||||||
public $routes = [];
|
public $routes = [];
|
||||||
public $query = [];
|
public $query = [];
|
||||||
public $path;
|
public $path;
|
||||||
public $controller;
|
public $controllerPath = BASEPATH . '/App/controllers/404.php';
|
||||||
public $controllerPath;
|
|
||||||
public $parameters = [];
|
|
||||||
public $requestType = 'get';
|
|
||||||
|
|
||||||
public function __construct() {
|
public function __construct() {
|
||||||
$this->routes = $this->loadRoutes();
|
$this->loadRoutes();
|
||||||
$this->path = $this->preparePath();
|
$this->preparePath();
|
||||||
$this->query = $this->prepareQuery();
|
$this->prepareQuery();
|
||||||
$this->requestType = $this->getRequestType();
|
$this->setRouteFile();
|
||||||
$this->controller = $this->findController();
|
|
||||||
$this->controllerPath = $this->setRouteFile();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private function loadRoutes() {
|
private function loadRoutes() {
|
||||||
require_once( BASEPATH . '/App/routes.php');
|
require_once( BASEPATH . '/App/routes.php');
|
||||||
return $routes;
|
$this->routes = $routes;
|
||||||
}
|
}
|
||||||
|
|
||||||
private function preparePath() {
|
private function preparePath() {
|
||||||
$path = parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH);
|
$this->path = parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH);
|
||||||
|
|
||||||
//homepage
|
|
||||||
if ($path === '/') {
|
|
||||||
return $path;
|
|
||||||
}
|
|
||||||
|
|
||||||
// remove empty directory path
|
|
||||||
$path = rtrim($path, '/'); // remove trailing slash
|
|
||||||
|
|
||||||
//remove anything after and including ampersand
|
|
||||||
$path = preg_replace('/&.+$/', '', $path);
|
|
||||||
|
|
||||||
return $path;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private function prepareQuery() {
|
private function prepareQuery() {
|
||||||
@ -46,83 +28,16 @@ class Router {
|
|||||||
if (isset($parsedUri['query'])) {
|
if (isset($parsedUri['query'])) {
|
||||||
parse_str($parsedUri['query'], $queryArray);
|
parse_str($parsedUri['query'], $queryArray);
|
||||||
}
|
}
|
||||||
return $queryArray;
|
$this->query = $queryArray;
|
||||||
}
|
}
|
||||||
|
|
||||||
private function getRequestType() {
|
|
||||||
// is the requewst a get or post?
|
|
||||||
if (empty($_POST)) {
|
|
||||||
return 'get';
|
|
||||||
} else {
|
|
||||||
return 'post';
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private function findController() {
|
|
||||||
|
|
||||||
// one to one match
|
|
||||||
if (array_key_exists($this->path, $this->routes)) {
|
|
||||||
return $this->routes[$this->path][$this->requestType];
|
|
||||||
}
|
|
||||||
|
|
||||||
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][$this->requestType];
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return '404';
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
// checks if the file exists, sets file path
|
|
||||||
private function setRouteFile() {
|
private function setRouteFile() {
|
||||||
|
foreach ($this->routes as $key => $value) {
|
||||||
$cp = BASEPATH . '/App/controllers/' . $this->controller . '.php';
|
if ( $this->path == $key) {
|
||||||
|
$this->controllerPath = BASEPATH . '/App/controllers/' . $value['file'] . '.php';
|
||||||
if (file_exists($cp)) {
|
break;
|
||||||
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,51 +0,0 @@
|
|||||||
<?php
|
|
||||||
|
|
||||||
class Session {
|
|
||||||
private $session;
|
|
||||||
|
|
||||||
public function __construct() {
|
|
||||||
if (!isset($_SESSION)) {
|
|
||||||
session_start();
|
|
||||||
$this->session = $_SESSION;
|
|
||||||
} else {
|
|
||||||
$this->session = $_SESSION;
|
|
||||||
}
|
|
||||||
$this->setToken();
|
|
||||||
}
|
|
||||||
|
|
||||||
public function setToken() {
|
|
||||||
if (!isset($this->session['token'])) {
|
|
||||||
$this->session['token'] = bin2hex(random_bytes(32));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public function set($key, $value) {
|
|
||||||
$this->session[$key] = $value;
|
|
||||||
}
|
|
||||||
|
|
||||||
public function get($key) {
|
|
||||||
return isset($this->session[$key]) ? $this->session[$key] : null;
|
|
||||||
}
|
|
||||||
|
|
||||||
public function flash($key) {
|
|
||||||
$return = $this->get($key);
|
|
||||||
$this->delete($key);
|
|
||||||
return $return;
|
|
||||||
}
|
|
||||||
|
|
||||||
public function debug() {
|
|
||||||
print_r($this->session);
|
|
||||||
}
|
|
||||||
|
|
||||||
public function delete($key) {
|
|
||||||
if (isset($this->session[$key])) {
|
|
||||||
unset($this->session[$key]);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public function write() {
|
|
||||||
$_SESSION = $this->session;
|
|
||||||
session_write_close();
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
13
src/bootstrap.php
Normal file
13
src/bootstrap.php
Normal file
@ -0,0 +1,13 @@
|
|||||||
|
<?php
|
||||||
|
require_once(BASEPATH . '/vendor/autoload.php');
|
||||||
|
|
||||||
|
//Twig
|
||||||
|
$loader = new Twig\Loader\FilesystemLoader(BASEPATH . '/App/views/');
|
||||||
|
$loader->addPath(BASEPATH . '/vendor/4lt/novaconium/twig', 'novaconium');
|
||||||
|
$loader->addPath(BASEPATH . '/App/templates', 'override');
|
||||||
|
$twig = new Twig\Environment($loader);
|
||||||
|
|
||||||
|
// Load a controller
|
||||||
|
require_once('Router.php');
|
||||||
|
$router = new Router();
|
||||||
|
require_once($router->controllerPath);
|
@ -1,23 +0,0 @@
|
|||||||
<?php
|
|
||||||
require_once(BASEPATH . '/vendor/autoload.php');
|
|
||||||
require_once(BASEPATH . '/App/config.php');
|
|
||||||
|
|
||||||
// Creates twig and the view() function
|
|
||||||
require_once(BASEPATH . '/vendor/4lt/novaconium/src/twig.php');
|
|
||||||
|
|
||||||
// Start a Session
|
|
||||||
require_once(BASEPATH . '/vendor/4lt/novaconium/src/Session.php');
|
|
||||||
$session = new Session();
|
|
||||||
|
|
||||||
// Load Database Class
|
|
||||||
require_once(BASEPATH . '/vendor/4lt/novaconium/src/Database.php');
|
|
||||||
$db = new Database($config['database']);
|
|
||||||
|
|
||||||
// Load a controller
|
|
||||||
require_once(BASEPATH . '/vendor/4lt/novaconium/src/Router.php');
|
|
||||||
$router = new Router();
|
|
||||||
//$router->debug();
|
|
||||||
require_once($router->controllerPath);
|
|
||||||
|
|
||||||
//write the session
|
|
||||||
$session->write();
|
|
17
src/twig.php
17
src/twig.php
@ -1,17 +0,0 @@
|
|||||||
<?php
|
|
||||||
|
|
||||||
//Twig
|
|
||||||
function view($name = '', $data = [] ) {
|
|
||||||
$loader = new Twig\Loader\FilesystemLoader(BASEPATH . '/App/views/');
|
|
||||||
$loader->addPath(BASEPATH . '/vendor/4lt/novaconium/twig', 'novaconium');
|
|
||||||
$loader->addPath(BASEPATH . '/App/templates', 'override');
|
|
||||||
$twig = new Twig\Environment($loader);
|
|
||||||
//check if file exists
|
|
||||||
if (file_exists(BASEPATH . '/App/views/' . $name . '.html.twig')) {
|
|
||||||
echo $twig->render("$name" . '.html.twig', $data);
|
|
||||||
return true;
|
|
||||||
} else {
|
|
||||||
echo "Error: Twig Template ($name) Not Found.";
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
}
|
|
Loading…
Reference in New Issue
Block a user