Skip to content

Instantly share code, notes, and snippets.

@onliniak
Last active September 16, 2019 14:14
Show Gist options
  • Save onliniak/76f0f8b39acfb4bf1f88342b1b9c62fd to your computer and use it in GitHub Desktop.
Save onliniak/76f0f8b39acfb4bf1f88342b1b9c62fd to your computer and use it in GitHub Desktop.
[PHP] simple MVC router and public directory (Appache)
#public/.htaccess
RewriteEngine On
RewriteBase /public/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?$1 [L,QSA]
#.htaccess
RewriteEngine On
RewriteCond %{THE_REQUEST} /public/([^\s?]*) [NC]
RewriteRule ^ %1 [L,NE,R=302]
RewriteRule ^((?!public/).*)$ public/$1 [L,NC]
#https://stackoverflow.com/questions/23635746/htaccess-redirect-from-site-root-to-public-folder-hiding-public-in-url
<?php
/**
* Absolute patch
* @param string $dir Relative patch (from root directory)
* @return string Get absolute patch
*/
function include_dir($dir = null)
{
$outside_directory = $_SERVER['DOCUMENT_ROOT'];
include $outside_directory.$dir;
}
/**
* Ultra simple router. Automatically show files from /src/controllers.
* For example domain.com/test → load file root_directory/src/controllers/test.php
*/
function router_get()
{
$url = $_SERVER['REQUEST_URI'];
include_dir($dir = '/src/controllers/'.$url.'.php');
}
/**
* [ßeta] Router 1.5. Manually load POST files.
* @param string $url $_SERVER['REQUEST_URI']
* @param string $method HTTP CRUD (POST/GET/DELETE/PUT), default POST
* @return string include root_directory/METHOD/URL for example function router($url = 'test') → load file from root_directory/src/controllers/POST/test.php
*/
function router($url = null, $method = 'POST')
{
if (($_SERVER['REQUEST_METHOD'] == $method) && (strpos($_SERVER['REQUEST_URI'], $url))) {
include_dir($dir = '/src/controllers/'.$method.'/'.$url.'.php');
}
}
/**
* Primitive autoloader, loads css/js/php files based on /src/models/load.json
* @param string $type css|js
* @param string $what name of file
* @param string $where (optional) if $where == $_SERVER['REQUEST_URI']
* @return string Load assets, for example <link rel="stylesheet" type="text/css" href="css/test'>, if $type = 'css' or <script src="js/test.js">, if $type = 'js'.
*/
function load_me($type = '', $what = "", $where = null)
{
$load_me = $_SERVER['DOCUMENT_ROOT'].'/src/models/load.json';
$json_file = file_get_contents($load_me);
$json_data = json_decode($json_file, true);
#array_push($json_data, $array);
if ($type = 'css') {
foreach ($json_data[$type][$what] as $key => $value) {
echo '<link rel="stylesheet" type="text/css" href="css/';
echo $value;
echo '.css">';
}
}
if ($type = 'js') {
foreach ($json_data[$type][$what] as $key => $value) {
echo '<script src="';
echo $value;
echo '.js"></script>';
}
}
}
#public/index.php
<?php
require '../src/functions.php';
require '../src/template.php';
router_get();
#src/models/load.json
{
"css":{
"header":[
"test"
]
},
"js":{
"footer":[
"test"
]
}
}
app
- .htaccess
- public/
- .htaccess
- index.php
- css/
- test.css
- js/
- test.js
- src/
- functions.php
- template.php
- models/
- load.json
- controllers/
- test.php
- POST/
- test.php
#src/template.php
<!DOCTYPE html>
<html lang="pl-PL">
<head>
<meta charset="UTF-8">
<title>Title of the document</title>
<?php
load_me($type = 'css', $where = "header");
#load_me($type = 'js', $where = "header");
?>
</head>
<body>
<?php
#load_me($type = 'css', $where = "footer");
load_me($type = 'js', $where = "footer");
?>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment