Skip to content

Instantly share code, notes, and snippets.

@xdemocle
Created August 17, 2016 23:43
Show Gist options
  • Save xdemocle/84568bd1143242e8bd7cd91113e362f3 to your computer and use it in GitHub Desktop.
Save xdemocle/84568bd1143242e8bd7cd91113e362f3 to your computer and use it in GitHub Desktop.
Local routing for PHP debug built-in server
<?php
// routing.php
if (preg_match('/\.(?:png|jpg|jpeg|gif|svg|html)$/', $_SERVER["REQUEST_URI"])) {
$isNotInPublicFoler = preg_match('/^((?!app|css).)*$/s', $_SERVER["REQUEST_URI"]);
if ($isNotInPublicFoler) {
$file_requested = $_SERVER["DOCUMENT_ROOT"] . $_SERVER["REQUEST_URI"];
} else {
$file_requested = str_replace('/public', '', $_SERVER["DOCUMENT_ROOT"]) . $_SERVER["REQUEST_URI"];
}
$file_mime = mime_content_type($file_requested);
header('Content-Type: ' . $file_mime . '; charset=utf-8');
return readfile($file_requested);
} else if (preg_match('/\.(?:css)$/', $_SERVER["REQUEST_URI"])) {
header('Content-Type: text/css; charset=utf-8');
include __DIR__ . $_SERVER["REQUEST_URI"];
} else if (preg_match('/\.(?:js)$/', $_SERVER["REQUEST_URI"])) {
header('Content-Type: application/x-javascript; charset=utf-8');
include __DIR__ . $_SERVER["REQUEST_URI"];
} else if ($_SERVER["REQUEST_URI"] === '/') {
include __DIR__ . '/public/index.php';
} else {
if (file_exists(__DIR__ . '/public' . $_SERVER["REQUEST_URI"])) {
include __DIR__ . '/public' . $_SERVER["REQUEST_URI"];
exit;
}
include __DIR__ . '/public/index.php';
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment