Compare commits

...

14 Commits

Author SHA1 Message Date
0d4b62ffb9 updated defaults from examples 2024-09-27 12:58:19 -07:00
7cd3071905 router tweak 2024-09-17 22:21:02 -07:00
77b60d34f6 fixed router typos 2024-09-16 06:54:02 -07:00
b858af3b55 updated routes for get and post 2024-09-16 06:47:00 -07:00
937e4581ba added session, moved twig, updated base 2024-09-12 16:28:33 -07:00
f19f59d53a fixed homepage 2024-09-06 12:53:25 -07:00
0c96ba6c90 fixed up router 2024-09-06 12:45:37 -07:00
713a44ad98 fixed some bugs 2024-09-02 17:10:24 -07:00
ea40fc8e38 Moved bootstrap to novaconium 2024-09-01 17:17:12 -07:00
8190b5ed71 updated 404 page 2024-08-31 15:52:59 -07:00
dca2556bca bumped version 2024-08-31 15:30:16 -07:00
90fd87d042 added urls 2024-08-31 15:28:13 -07:00
9447e76815 working for a parked page 2024-08-31 14:02:51 -07:00
e64fbc0678 twig override tweaks 2024-08-30 01:18:08 -07:00
22 changed files with 370 additions and 82 deletions

View File

@ -6,6 +6,9 @@ NovaconiumPHP is a high-performance PHP framework designed with inspiration from
Pronounced: Noh-vah-koh-nee-um Pronounced: Noh-vah-koh-nee-um
Packagist: https://packagist.org/packages/4lt/novaconium
Master Repo: https://git.4lt.ca/4lt/novaconium
## Getting Started ## Getting Started
### Installation ### Installation
@ -13,8 +16,7 @@ Pronounced: Noh-vah-koh-nee-um
```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/ .
``` ```

View File

@ -1,7 +1,7 @@
{ {
"name": "4lt/novaconium", "name": "4lt/novaconium",
"description": "A high-performance PHP framework built from the past.", "description": "A high-performance PHP framework built from the past.",
"version": "1.0.0", "version": "1.0.1",
"license": "MIT", "license": "MIT",
"authors": [ "authors": [
{ {
@ -26,4 +26,4 @@
} }
} }
} }

11
defaults/App/config.php Normal file
View File

@ -0,0 +1,11 @@
<?php
$config = [
'database' => [
'host' => '',
'name' => '',
'user' => '',
'pass' => '',
'port' => 3306
],
'base_url' => 'http://localhost:8000'
];

View File

@ -0,0 +1,15 @@
<?php
// 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>
<p style="font-size:10px; margin-top:60px">Novaconium Default 404 page.</p>

View File

@ -1,9 +1,9 @@
<?php <?php
$routes = [ $routes = [
'/about' => [ '/about' => [
'file' => 'about' 'get' => 'about'
], ],
'/' => [ '/' => [
'file' => 'index' 'get' => 'index'
] ]
]; ];

View File

@ -0,0 +1 @@
{# Overrides go here #}

View File

@ -1,4 +1,4 @@
{% extends '@nytwig/master.html.twig' %} {% extends '@novaconium/master.html.twig' %}
{% block content %} {% block content %}
<h1>This is twig</h1> <h1>This is twig</h1>

View File

@ -1,6 +1,4 @@
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]

View 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/novaconium.php');
?>

View File

@ -1 +0,0 @@
<h1>This is 404</h1>

View File

@ -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/bootstrap.php');
?>

75
src/Database.php Normal file
View 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();
}
}

View File

@ -4,22 +4,45 @@ class Router {
public $routes = []; public $routes = [];
public $query = []; public $query = [];
public $path; public $path;
public $controllerPath = BASEPATH . '/App/controllers/404.php'; public $controller;
public $controllerPath;
public $parameters = [];
public $requestType = 'get';
public function __construct() { public function __construct() {
$this->loadRoutes(); $this->routes = $this->loadRoutes();
$this->preparePath(); $this->path = $this->preparePath();
$this->prepareQuery(); $this->query = $this->prepareQuery();
$this->setRouteFile(); $this->requestType = $this->getRequestType();
$this->controller = $this->findController();
$this->controllerPath = $this->setRouteFile();
} }
private function loadRoutes() { private function loadRoutes() {
require_once( BASEPATH . '/App/routes.php'); // Check if Path exists
$this->routes = $routes; if (file_exists(BASEPATH . '/App/routes.php')) {
require_once( BASEPATH . '/App/routes.php');
} else {
require_once(FRAMEWORKPATH . '/defaults/App/routes.php');
}
return $routes;
} }
private function preparePath() { private function preparePath() {
$this->path = parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH); $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() {
@ -28,16 +51,87 @@ class Router {
if (isset($parsedUri['query'])) { if (isset($parsedUri['query'])) {
parse_str($parsedUri['query'], $queryArray); parse_str($parsedUri['query'], $queryArray);
} }
$this->query = $queryArray; return $queryArray;
} }
private function setRouteFile() { 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) { foreach ($this->routes as $key => $value) {
if ( $this->path == $key) { // Check if key contains a curly bracket, if not continue. We already checked above.
$this->controllerPath = BASEPATH . '/App/controllers/' . $value['file'] . '.php'; if (!strpos($key, '{')) continue;
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][$this->requestType];
}
}
}
}
}
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 {
//Check if 404 exits
if (file_exists(BASEPATH . '/App/controllers/404.php')) {
return BASEPATH . '/App/controllers/404.php';
} else {
return FRAMEWORKPATH . '/defaults/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();
}
} }

51
src/Session.php Normal file
View File

@ -0,0 +1,51 @@
<?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();
}
}

View File

@ -1,13 +0,0 @@
<?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);

31
src/novaconium.php Normal file
View File

@ -0,0 +1,31 @@
<?php
define('FRAMEWORKPATH', BASEPATH . '/vendor/4lt/novaconium');
require_once(BASEPATH . '/vendor/autoload.php');
//Check if config file exists
if (file_exists(BASEPATH . '/App/config.php')) {
require_once(BASEPATH . '/App/config.php');
} else {
require_once(FRAMEWORKPATH . '/defaults/App/config.php');
}
// Creates twig and the view() function
require_once(FRAMEWORKPATH . '/src/twig.php');
// Start a Session
require_once(FRAMEWORKPATH . '/src/Session.php');
$session = new Session();
// Load Database Class
require_once(FRAMEWORKPATH . '/src/Database.php');
$db = new Database($config['database']);
// Load a controller
require_once(FRAMEWORKPATH . '/src/Router.php');
$router = new Router();
//$router->debug();
require_once($router->controllerPath);
//write the session
$session->write();

17
src/twig.php Normal file
View File

@ -0,0 +1,17 @@
<?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;
}
}

6
twig/foot.html.twig Normal file
View File

@ -0,0 +1,6 @@
<!--
What goes very last on the page.
right before the /body
like javascript
or analytics
-->

3
twig/footer.html.twig Normal file
View File

@ -0,0 +1,3 @@
<!--
What goes in the footer html tag
-->

View File

@ -1,6 +1,6 @@
<meta charset="utf-8"> <meta charset="utf-8">
<title>{{ title | default('Welcome') }}</title> <title>{{ title | default('Welcome') }}</title>
<meta name="generator" content="nickyeoman/phpframework" /> <meta name="generator" content="Novaconium" />
<meta name="description" content="{{ description | default('No description given') }}"> <meta name="description" content="{{ description | default('No description given') }}">
<meta name="keywords" content="{{ keywords | default('website') }}"> <meta name="keywords" content="{{ keywords | default('website') }}">

View File

@ -1,54 +1,52 @@
<!doctype html> <!doctype html>
<html class="no-js" lang="en"> <html class="no-js" lang="en">
{% include '@override/mod_above_head.html.twig' ignore missing %} {% include '@override/above_head.html.twig' ignore missing %}
<head> <head>
{% include ['@override/mod_head.html.twig', '@novaconium/head.html.twig'] %} {% include ['@override/head.html.twig', '@novaconium/head.html.twig'] %}
</head> </head>
<body id="{{ pageid | default('pageid') }}"> <body id="{{ pageid | default('pageid') }}">
{# Page Header #}
<header>
{% block headerbefore %}{% endblock %}
{% include ['@override/nav.html.twig', '@novaconium/nav.html.twig'] %}
{% block headerafter %}{% endblock %}
</header>
{# Page Header #} <!-- Main Content Of The Page -->
<header> <div id="page">
{% block headerbefore %}{% endblock %} <div class="container">
{% include ['@override/mod_nav.html.twig', '@novaconium/nav.html.twig'] %}
{% block headerafter %}{% endblock %}
</header>
<!-- Main Content Of The Page --> <div class="middle">
<div id="page"> {% if error|default is not empty %}
<div class="container"> {% for key, val in error %}
<div class="error">{{ val }}</div>
{% endfor %}
{% endif %}
<div class="middle"> {% if notice|default is not empty %}
{% if error|default is not empty %} {% for key, val in notice %}
{% for key, val in error %} <div class="notice">{{ val }}</div>
<div class="error">{{ val }}</div> {% endfor %}
{% endfor %} {% endif %}
{% endif %}
{% if notice|default is not empty %} <article>
{% for key, val in notice %} {% block content %}{% endblock %}
<div class="notice">{{ val }}</div> </article>
{% endfor %} </div>
{% endif %}
<article>
{% include 'cms/mod_alex.html.twig' ignore missing %}
{% block content %}{% endblock %}
{% include 'cms/mod_simon.html.twig' ignore missing %}
</article>
</div> </div>
</div> </div>
</div>
{# Page Footer #} {# Page Footer #}
<footer> <footer>
{% block footerbefore %}{% endblock %} {% block footerbefore %}{% endblock %}
{% include ['@override/mod_footer.html.twig', '@novaconium/footer.html.twig'] %} {% include ['@override/footer.html.twig', '@novaconium/footer.html.twig'] %}
{% block footerafter %}{% endblock %} {% block footerafter %}{% endblock %}
</footer> </footer>
{% include ['@override/mod_foot.html.twig', '@novaconium/foot.html.twig'] %} {% include ['@override/foot.html.twig', '@novaconium/foot.html.twig'] %}
</body></html> </body></html>