Service Static Files in Slim4 from built-in http server

Serving static files with Apache is trivial as mod_rewrite handles files that match.

RewriteEngine On
RewriteCond  "%{DOCUMENT_ROOT}/%{REQUEST_URI}" !-f  <-- IF THE FILE EXISTS, SERVE IT UP AND EXIT
RewriteCond  "%{DOCUMENT_ROOT}/%{REQUEST_URI}" !-d <-- IF THE DIRECTORY EXISTS, SERVE IT UP AND EXIT
RewriteRule ^ /index.php [QSA,L]   <-- ELSE, SEND THROUGH FRONT_END CONTROLLER

However, this is a bit more difficult when running app in PHPs built-in webserver. My directory structure is:

assets
– css
– js
– images

One option is to create “catch all” routes similiar this:

$app->get('/assets/{dir}/{filename}', \App\Action\StaticFile\StaticFileHandler::class);

and then stream the file contents (with appropriate header) from the Action class. That seems unnecessarily burdensome. Even then, it doesn’t capture things like favicon and the like.

What is the best way to serve static files from PHPs http server and for bonus points is it possible to turn off the routing when not served through the built-in server??

The “best” way would be to start the PHP development server directly within the public/ directory.

cd /path/to/project/public/
php -S localhost:8080

That did it! I was starting it in my public directory but using the command:

php -S localhost:8888 index.php