Skip to content

Instantly share code, notes, and snippets.

@cdsaenz
Created October 26, 2024 16:34
Show Gist options
  • Save cdsaenz/2fd7a703f716c68903632eff5f41888d to your computer and use it in GitHub Desktop.
Save cdsaenz/2fd7a703f716c68903632eff5f41888d to your computer and use it in GitHub Desktop.
NadaPHP Minimal Framework
<!DOCTYPE html>
<html lang="en">
<head>
<?= partial('parts/head', ['title' => '404 Not Found']) ?>
</head>
<body>
<?= partial('parts/navbar') ?>
<main class="container text-center mt-5">
<div class="p-4 mt-4">
<h1 class="display-6 text-danger">404 - Page Not Found</h1>
<p class="lead">Sorry, the page you're looking for doesn't exist or may have been moved.</p>
<a href="<?= url('/') ?>">Return Home</a>
</div>
</main>
<?= partial('parts/footer') ?>
<?= partial('parts/script') ?>
</body>
</html>
<?php
/**
* Nada PHP
* Minimal Framework for an otherwise mostly static HTML site.
* Essentially a simple router with url handling and views/partials.
* - Deploy this file to your wwwroot
* - Adjust framework paths, create FWPATH/404.php
* - Create VIEWPATH/home.php
* - Routes are automatic ie site.com/my-blog will look for VIEWPATH/my-blog.php
* - Full path routes like /blog/archive/list will look for VIEWPATH/archive/list.php
* - A FWPATH/app directory is declared in case of needing more logic.
* 1.0 - Oct2024 - Beta Test Version
* CSDev
*/
define('ROOTPATH', __DIR__);
/* Set content paths securely out of wwwroot for deployment! */
define('FWPATH' , ROOTPATH . DIRECTORY_SEPARATOR . 'framework' );
define('APPPATH' , ROOTPATH . DIRECTORY_SEPARATOR . 'app' );
define('VIEWPATH', FWPATH . DIRECTORY_SEPARATOR . 'design');
/* Set URL to your wwwroot, it can be a subfolder */
define('BASEURL', 'https://example.com/folder');
/* Development Error log */
error_reporting(E_ALL);
ini_set('display_errors', 1);
ini_set('log_errors', 1);
ini_set('error_log', FWPATH . DIRECTORY_SEPARATOR . 'data/logs/error.log');
/* Nice Exception Display */
set_exception_handler('exceptionHandler');
/* Verify environment */
if (!is_dir(VIEWPATH) || !file_exists(VIEWPATH . '/404.php')) {
throw new Exception(VIEWPATH . " not found or 404.php missing");
}
/* Simple Router */
$route = currentURLRoute();
$data = array();
switch ($route) {
case '/':
view("home", $data);
break;
default:
view($route, $data);
break;
}
/** eom */
/**
* Get the complete current URL
*/
function currentURL() : string
{
return (empty($_SERVER['HTTPS']) ? 'http' : 'https') . "://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
}
/**
* Return path
*/
function currentURLRoute() : string
{
$url = str_replace(BASEURL, '', currentURL());
return strtok($url, '?');
}
/**
* Get full url route
*/
function url($route): string
{
return BASEURL . '/' . ltrim($route, '/');
}
/**
* Assets url with versioning
*/
function assetURL($filename,$version = false): string
{
$asset = BASEURL . '/assets/' . $filename;
if ($version) {
if ($version === true) {
$version = date('YmdHis');
}
$asset .= '?v=' . $version;
}
return $asset;
}
/**
* Loads a view file with optional data and can return the content.
*/
function view(string $filename, array $data = [], bool $return = false): ?string {
// sanitize filename
$filename = preg_replace('/[^a-zA-Z0-9\/_-]/', '', $filename);
$filepath = VIEWPATH . "/" . $filename . ".php";
if (!file_exists($filepath)) {
return render404();
}
extract($data);
ob_start();
include $filepath;
$content = ob_get_clean();
if ($return) {
return $content;
}
echo $content;
return null;
}
/**
* Render a 404 page with the appropriate HTTP status code.
*/
function render404(array $data = []): ?string {
http_response_code(404);
return view("404", $data);
}
/**
* Simplified for template use
*/
function partial(string $filename, array $data = []): string {
return view($filename, $data, true);
}
/**
* Exception handler
*/
function exceptionHandler($exception)
{
http_response_code(500);
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Exception Error</title>
</head>
<body>
<div style="padding: 20px;background-color: #211919;color: white;border-radius: 9px;border-left: 6px solid red;">
<h1 style="color: orangered">An Exception error has been produced.</h1>
<p><?= htmlspecialchars($exception->getMessage()) ?></p>
</div>
<div style="margin-top: 10px">
File : <?= htmlspecialchars($exception->getFile()) ?><br>
Line : <?= htmlspecialchars($exception->getLine()) ?><br>
Trace : <?= nl2br(htmlspecialchars($exception->getTraceAsString())) ?><br>
</div>
</body>
</html>
<?php
exit();
}
Demo structure
- Move framework off wwwroot and adjust paths in index.php on deploy
nada (under wwwroot)
├── assets
│ ├── css
│ │ └── style.css
│ └── img
├── framework
│ ├── app
│ ├── data
│ │ └── logs
│ └── design
│ ├── 404.php
│ ├── about.php
│ ├── blog
│ │ └── posts.php
│ ├── home.php
│ └── parts
│ ├── footer.php
│ ├── head.php
│ ├── navbar.php
│ └── script.php
└── index.php
└── .htaccess
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment