Skip to content

Instantly share code, notes, and snippets.

@ccdd13
Forked from ckressibucher/cliserver.php
Created April 26, 2021 08:47
Show Gist options
  • Save ccdd13/be727a5bea1373ffa71d4d78c36b0737 to your computer and use it in GitHub Desktop.
Save ccdd13/be727a5bea1373ffa71d4d78c36b0737 to your computer and use it in GitHub Desktop.
Router script for PHP built in server

Project layout:

$ /t/project> tree .
.
├── public
│   ├── cliserver.php
│   └── index.php
├── src
└── test

Start dev server:

cd public && php -S 127.0.0.1:8080 cliserver.php

For some more details about routing with the PHP built in development server, see my article

<?php
// public/cliserver.php (router script)
if (php_sapi_name() !== 'cli-server') {
die('this is only for the php development server');
}
if (is_file($_SERVER['DOCUMENT_ROOT'].'/'.$_SERVER['SCRIPT_NAME'])) {
// probably a static file...
return false;
}
$_SERVER['SCRIPT_NAME'] = '/index.php';
// if needed, fix also 'PATH_INFO' and 'PHP_SELF' variables here...
// require the entry point
require 'index.php';
<?php
// public/index.php (the normal entry point for your web app)
print_r($_SERVER);
//$app = new App();
//$app->run();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment