phprouter / main

Secure router with XSS and CSRF
MIT License
514 stars 143 forks source link

Make PHP router work easily with built-in php test server #40

Closed Largo closed 1 year ago

Largo commented 2 years ago

The simplest way to test a PHP application is to use the built-in PHP Server. It works quite well, but it needs to know if the router script didn't find a result and if it needs to check for a file. This works by not echoing anything and returning false.

Easiest way is to change this in the routes.php file. You could add a check if a file exists or not to make the 404 page work if needed.


if (php_sapi_name() != 'cli-server') {
    any('/404','views/404.php');
} else {
    return false;
}

php -S 127.0.0.1:8000 routes.php

Edit: If the routes capture destinations where files exist, then this won't work. Alternatively placing the following at the beginning of routes.php would help. This might need more input sanitization before it can be used:


if (php_sapi_name() === 'cli-server') {
    $fileName = __DIR__.parse_url($_SERVER["REQUEST_URI"], PHP_URL_PATH);
    if (file_exists($fileName) && !is_dir($fileName)) return false;
}
phprouter commented 1 year ago

This is beyond the job of the router.