Skip to content

Instantly share code, notes, and snippets.

@agbaraka
Forked from ckressibucher/cliserver.php
Created July 7, 2017 08:26
Show Gist options
  • Save agbaraka/72e6586e1885a9468693bafe813423a4 to your computer and use it in GitHub Desktop.
Save agbaraka/72e6586e1885a9468693bafe813423a4 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