Skip to content

Instantly share code, notes, and snippets.

@uda
Last active August 29, 2015 14:06
Show Gist options
  • Save uda/7caadceb08976a76e37f to your computer and use it in GitHub Desktop.
Save uda/7caadceb08976a76e37f to your computer and use it in GitHub Desktop.
PHP built-in server router
<?php
// router.php
if (PHP_SAPI == 'cli-server') {
$path = parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH);
if ($path[0] == '/') {
$path = substr($path, 1);
}
if (!is_file($path) && !is_dir($path)) {
// Fixes include issues when running from another directory than the router
chdir(__DIR__);
require 'index.php';
} else {
return false;
}
}
return true;
@xeoncross
Copy link


if (php_sapi_name() == 'cli-server') {
  $path = trim(parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH), '/') . '/';

  if ( ! file_exists($path)) {
    require 'index.php';
  }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment