Improved kernel routing system

This commit is contained in:
Niko 2022-02-10 02:39:26 +01:00
parent a3c0ac7ab6
commit 8718c099f8
4 changed files with 39 additions and 11 deletions

3
web/src/app/app.php Normal file
View File

@ -0,0 +1,3 @@
<?php
echo 'APP';

12
web/src/config/routes.php Normal file
View File

@ -0,0 +1,12 @@
<?php
global $routes;
$routes = [
'root' => [
'/' => 'public.home',
],
'app' => [
'/' => 'app.app',
'/login' => 'public.login',
],
];

View File

@ -13,23 +13,33 @@ if ($env === PROD) {
ini_set('display_errors', 0);
}
$uri = rtrim(trim($_SERVER['REQUEST_URI']),'/');
$s_uri = preg_replace('/^'.preg_quote(
$uri = rtrim(trim($_SERVER['REQUEST_URI']),'/');
$app_uri = preg_replace('/^'.preg_quote(
APP_DIR,'/').'\//', '/', $uri);
if ($app_uri === APP_DIR)
$app_uri = '/';
function req_pub($name) { require "public/$name.php"; die; }
function redirect($url) { header('Location: '.$url); die; }
# Public URLs
switch ($s_uri)
{
case '/': req_pub('home');
case '/login': req_pub('login');
function module($ns, $die = false) {
require_once str_replace('.','/',$ns).'.php';
if ($die) die;
}
# Example: /@username
################
# module ROUTING
module('config.routes');
foreach ($routes['root'] as $path => $module)
if ($uri === $path)
module($module, true);
foreach ($routes['app'] as $path => $module)
if ($app_uri === $path)
module($module, true);
# /@username
if (preg_match('/^\/\@[a-zA-Z0-9\-\_]+/', $uri))
{
if ($_SERVER['HTTP_ACCEPT'] === 'application/activity+json')

3
web/src/public/home.php Normal file
View File

@ -0,0 +1,3 @@
<?php
echo 'HOME';