-
Notifications
You must be signed in to change notification settings - Fork 0
Static File Links
Muhammet Şafak edited this page Jun 9, 2026
·
1 revision
link() exposes a file — or all files inside a directory — at a URL path. The
file is streamed into the response with appropriate Content-Type and
Content-Length headers.
$router->link('/favicon.ico', __DIR__ . '/assets/favicon.ico');A request to /favicon.ico serves that file.
When the source is a directory, requests below the link path map to files inside it:
$router->link('/assets', __DIR__ . '/public/assets');-
/assets/css/app.css→public/assets/css/app.css -
/assets/img/logo.png→public/assets/img/logo.png
Directory links are protected against traversal: the resolved real path must
stay inside the link root. A request such as /assets/../secret.txt does not
match and falls through to a 404 — it can never escape the directory.
-
link()throwsInvalidArgumentExceptionif the source path does not exist. - Link routes are considered for every HTTP method.
- A request that matches a link but whose target file is missing falls through to 404 handling (see Error Handling).
Next: Error Handling.
initphp/router · MIT License · part of the InitPHP family
Source · Issues · Discussions · Packagist · Contributing · Security Policy
Getting Started
Defining Routes
Handling Requests
Reference
Migration