Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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();
@tibinvpaul

This comment has been minimized.

Copy link

commented Aug 11, 2017

Thanks for this gist! It saved me big time!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.