Skip to content

Instantly share code, notes, and snippets.

@ckressibucher
Last active November 10, 2023 17:58
Show Gist options
  • Save ckressibucher/e37520cf2f1d08ec56d250c54d96ed72 to your computer and use it in GitHub Desktop.
Save ckressibucher/e37520cf2f1d08ec56d250c54d96ed72 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();
@tibinvpaul
Copy link

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