Skip to content

Core\Response::Redirect

Which kind of redirect a response is, when it answers by sending the visitor somewhere else.

Core\Response::Redirect is declared in the class Core\Response. The method redirect takes it as its status argument.

There are three. SeeOther sends the browser to fetch another page with a plain GET, whatever method asked — the answer to a saved form, and what you get when you name no case at all. Temporary asks for the same request again, method and body intact, at the new address this once. Permanent asks for the same request again as well, and says the new address is the one from now on, so caches and search engines are entitled to remember it.

Good to know: the two redirects everybody knows, 301 and 302, are missing on purpose. What a browser does with a posted form under those was settled by habit rather than by the standard, so what happens depends on who is asking. The three here mean the same thing everywhere, and any other status is still yours to write by hand.

Examples

The three redirects and what each asks for

<?nvs
// This program prints the three kinds of redirect and what the browser does after each one.
// They differ in two ways. Does the browser send the same request again?
// Is the new address the one to use from now on?
class Redirects
{
public static function label(Core\Response::Redirect $kind): string
{
return match ($kind) {
Core\Response::Redirect::SeeOther => 'SeeOther ',
Core\Response::Redirect::Temporary => 'Temporary',
Core\Response::Redirect::Permanent => 'Permanent',
};
}
// `Temporary` and `Permanent` make the browser send the request again, with the same method and body.
// `SeeOther` makes the browser load the new page with a plain `GET`.
public static function repeatsTheRequest(Core\Response::Redirect $kind): bool
{
return match ($kind) {
Core\Response::Redirect::SeeOther => false,
Core\Response::Redirect::Temporary => true,
Core\Response::Redirect::Permanent => true,
};
}
// Only `Permanent` tells the browser that the old address is gone for good.
public static function isForGood(Core\Response::Redirect $kind): bool
{
return match ($kind) {
Core\Response::Redirect::SeeOther => false,
Core\Response::Redirect::Temporary => false,
Core\Response::Redirect::Permanent => true,
};
}
}
array<Core\Response::Redirect> $kinds = [
Core\Response::Redirect::SeeOther,
Core\Response::Redirect::Temporary,
Core\Response::Redirect::Permanent,
];
foreach ($kinds as Core\Response::Redirect $kind) {
echo Redirects::label($kind),
' repeats the request: ', Redirects::repeatsTheRequest($kind) ? 'yes' : 'no ',
' the new address is the one from now on: ', Redirects::isForGood($kind) ? 'yes' : 'no',
"\n";
}
Output
SeeOther repeats the request: no the new address is the one from now on: no
Temporary repeats the request: yes the new address is the one from now on: no
Permanent repeats the request: yes the new address is the one from now on: yes

Answering a form that was just saved

<?nvs
// This program shows how to reply to a saved form with a redirect.
// If the reply were the page itself, a refresh would post the form again.
// A redirect sends the browser to a page that is safe to reload.
string $order = 'ORD-1042';
// After a `SeeOther` redirect, the browser loads the new page with a plain `GET`.
// This is what you want to show the saved order.
Core\Response::redirect('/orders/' . $order, Core\Response::Redirect::SeeOther);
// This is the short way to write the same redirect. `SeeOther` is the default.
// The second call replaces the first.
Core\Response::redirect('/orders/' . $order);
echo 'saved ', $order, ' and sent the browser to /orders/', $order, "\n";
// A refresh of the new page only loads the order again.
// The order is saved once.
echo "a refresh re-reads the order instead of posting the form again\n";
Output
saved ORD-1042 and sent the browser to /orders/ORD-1042
a refresh re-reads the order instead of posting the form again

Old addresses that are still being visited

<?nvs
// The everyday job: addresses that used to work and now point somewhere else.
// A page that was renamed is gone for good, so its answer says so and search
// engines relearn it. A page that is only away while it is being worked on is
// temporary, and nobody should remember the detour.
// What the visitor asked for. In a real program this comes from the request.
string $asked = '/shop/coffee-grinder';
array<string> $renamed = ['/shop/coffee-grinder' => '/shop/grinders/hand-crank'];
array<string> $awayForNow = ['/shop/sale' => '/shop'];
if (Core\Arr::hasKey($renamed, $asked)) {
// The new address is the address from now on, so a link somebody else
// saved can be updated and a search result can be replaced.
Core\Response::redirect($renamed[$asked], Core\Response::Redirect::Permanent);
echo 'moved for good, to ', $renamed[$asked], "\n";
} elseif (Core\Arr::hasKey($awayForNow, $asked)) {
// The old address is coming back, so nothing is asked to remember this.
Core\Response::redirect($awayForNow[$asked], Core\Response::Redirect::Temporary);
echo 'away for now, sent to ', $awayForNow[$asked], "\n";
} else {
echo "nothing has moved, so answer the page itself\n";
}
Output
moved for good, to /shop/grinders/hand-crank