Redirect
Provides context-aware, base-relative, current-request, referer and absolute URL redirects through Core\Response.
URL resolution
Relative destinations in to() resolve against the current context URL; absolute URLs remain
unchanged. toBase() resolves against the application base URL.
Runtime termination
All redirect methods return never: they clear the response, set the redirect status and
Location
header, and terminate through Core\Response.
Referer caution
back() uses the HTTP referer when available and otherwise returns to the current URL. A
referer
is client-controlled and must not be used as a security decision.
Supported statuses
Supported codes are 300, 301, 302, 303,
307
and 308; invalid codes cause a RedirectException.
Example
$redirect = Registry::get(Redirect::class);
$redirect->toBase('login'); // Sends redirect and exits.
Public API Reference
Core\Redirect
Class & Registry
Class:
Core\Redirect
Extends:
Core\Redirect\Base
use Core\Redirect;
use Core\Registry;
$redirect = Registry::get(Redirect::class);
| Method | Description |
|---|---|
init():void |
Initializes the redirect controller. No additional initialization is currently required. |
to(string|null $url = null, int $statusCode = 302):never |
Redirects to the specified URL using a validated HTTP redirect status code. Relative URLs are resolved against the current context URL, absolute URLs remain unchanged, and an omitted URL redirects to the current request. Clears the response and terminates the runtime. |
self(int $statusCode = 302):never |
Redirects to the current request URL using the specified HTTP redirect status code and terminates the runtime. |
back(int $statusCode = 302):never |
Redirects to the URL provided by the HTTP Referer header. Falls back to the current request URL when no referer is available and terminates the runtime. |
toBase(string|null $url = null, int $statusCode = 302):never |
Redirects to a URL resolved against the configured application base URL, bypassing the current context URL. Clears the response and terminates the runtime. |