Skip to content

Instantly share code, notes, and snippets.

@razvanphp
Created November 21, 2019 21:05
Show Gist options
  • Save razvanphp/40e21a237d34be25d126846490e26544 to your computer and use it in GitHub Desktop.
Save razvanphp/40e21a237d34be25d126846490e26544 to your computer and use it in GitHub Desktop.
Yii2 built-in PHP server router file with dot support in URI + access logs output
<?php
// serve static files
$uri = urldecode(parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH));
if (file_exists(__DIR__ . '/web' . $uri)) {
return false;
}
// output access log
function output()
{
$prefix = '[' . date('D M j H:i:s Y') . '] ' . $_SERVER['REMOTE_ADDR'] . ':' . $_SERVER['REMOTE_PORT'];
$request_uri = $_SERVER['REQUEST_METHOD'] . ' ' . $_SERVER['REQUEST_URI'];
error_log("{$prefix} [" . http_response_code() . "]: ${request_uri}\n", 3, 'php://stderr');
}
// do it even when exceptions are thrown
register_shutdown_function(function () {
output();
});
// add missing $_SERVER variables
$_SERVER['SCRIPT_FILENAME'] = realpath('./web/index.php');
// this one is missing for URI with dots
if (!isset($_SERVER['PATH_INFO'])) {
$_SERVER['PATH_INFO'] = $_SERVER['REQUEST_URI'];
}
include './web/index.php';
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment