Serve static files

← All configuration scenarios

By default, even a request for /assets/app.css goes to your PHP handler. Add a static directory when you want Mini4Work to send the contents of a file directly to the browser.

Browser requests/assets/app.css
Server readspublic/assets/app.css

Publish one file

This example uses manifest mode. It suits assets produced once per build and works with standard PHP. Start in an application with mini4work/webserver installed.

1. Create the asset and its manifest

Shell
mkdir -p public/assets
printf 'body { color: navy; }\n' > public/assets/app.css
printf '["app.css"]\n' > public/assets/.m4w-manifest.json

The manifest lists the files you want to publish, using names relative to its own directory. Here it lists one file.

2. Map the URL to the directory

Save this as server.json next to public/. The prefix is the URL segment visitors use. The path is the directory on disk.

JSON
{
  "listen": { "port": 9000 },
  "static": {
    "directories": [
      {
        "prefix": "/assets",
        "path": "public/assets",
        "mode": "manifest"
      }
    ]
  }
}

3. Start the server

Shell
php vendor/mini4work/webserver/examples/server.php server.json

At startup, the server reads the manifest and checks its files. If startup fails, check that the manifest and every listed file exist under public/assets/.

Test the URL

In a second terminal, request the stylesheet.

Shell
curl -i http://127.0.0.1:9000/assets/app.css

Expect HTTP 200 with body { color: navy; } in the response. The server matches /assets, finds app.css in the manifest and serves the file without calling your handler.

Now request /assets/missing.css. A miss goes to PHP. With the bundled example you will see its JSON inspection response. In your application, your handler decides whether to return 404. If the first request also returns JSON, check the URL prefix and the manifest entry, then restart with the intended profile.

Choose a mode for your files

Files change only when you deploy → manifest
List each published file in the manifest. Keep that build immutable while workers serve it. Deploy a new directory and manifest together, then restart.
Your build also produces gzip or Brotli copies → precompressed
Include the original and its encoded copies, for example ["app.css", "app.css.gz"]. Visitors still request /assets/app.css. The server uses Accept-Encoding to choose an available representation. No PHP compression extension is required.
Files are replaced while the server is running → secure
The server looks up each file within the opened directory at request time. This mode requires enabled FFI on a supported system, including Linux 5.6+ with openat2 or macOS 13+ on supported architectures. Restart if you replace the directory itself.

Directory options

path and mode · required
Set these on each entry in static.directories. With its default empty list, all requests go to the application unless the legacy static.public_dir shorthand configures one secure directory.
prefix · default "/"
The longest matching URL prefix wins. Directories sharing a prefix are searched in list order. A GET or HEAD request can serve a recognized asset type. Other methods reach the application.
etag · default false
Enable it to send a content tag. A browser that already has that version can send If-None-Match and receive 304 without downloading the bytes again.
cache_control · optional
Sets the Cache-Control header for files in this directory. Use long immutable lifetimes for fingerprinted URLs where changing the contents also changes the filename.