Core\Response::redirect
Core\Response::redirect(
string $url,
Core\Response::Redirect $status = Core\Response::Redirect::SeeOther,
): voidSends the browser to another address. It sets a redirect status and the Location header with the new address. The browser then loads the new address by itself.
Core\Response::Redirect names the three kinds of redirect. SeeOther (303) is the default. Use it after a form is sent, so the browser loads the next page with GET. Temporary (307) and Permanent (308) repeat the same request at the new address. Permanent also tells the browser to use the new address from now on.
The address can be a full URL or a path such as /orders/42. It must not be empty, and it contains only printable ASCII characters. It must not start with two slashes or backslashes, such as //example.com, because a browser reads that as another host. For another host, write the scheme, such as https://example.com/. For any other address, redirect throws a LogicError and sets nothing. Text from a visitor as the address does not compile, because a visitor must not choose where the browser goes.
The examples show a page that moved, an address that is not allowed, and a form that saves an order.
Parameters
$urlstringThe address to send the browser to. It can be a full URL or a path such as/orders/42. It is not empty and contains only printable ASCII characters. Ataintedvalue is not allowed, because a visitor must not choose where the browser goes.$statusCore\Response::Redirectdefault Core\Response::Redirect::SeeOtherThe kind of redirect:SeeOther(303),Temporary(307) orPermanent(308). The default isSeeOther, which you use after a form is sent.
Return value
Returns void. Nothing. If you call it more than once, the last call wins. If the request fails with an error, the status is 500 and there is no Location header.
Errors
LogicError$urlis empty, or contains a character that is not printable ASCII, such as a newline. Or it starts with two slashes or backslashes, such as//example.com, which a browser reads as another host. Nothing is set.
Examples
A page that moved
<?nvs// Shows how to send visitors from an old address to a new one.
// The page `/pricing` is now at `/plans`. `Permanent` sets the status 308,// so the browser uses `/plans` from now on.Core\Response::redirect('/plans', Core\Response::Redirect::Permanent);echo "Status 308, Location: /plans\n";Status 308, Location: /plansAn address that is not allowed
<?nvs// Shows what happens when the address of a redirect contains a newline.
// A newline would end the `Location` header and start a new header.// So `redirect` throws a `LogicError`, and the response is not a redirect.try { Core\Response::redirect("/home\nSet-Cookie: admin=1"); echo "Redirected\n";} catch (LogicError $error) { echo "Not redirected: the address contains a newline.\n";}Not redirected: the address contains a newline.A form that saves an order
<?nvs// Shows the usual way to end a form: save the data, then send the browser to the new page.
// The order is saved, and the new order has the number 42.uint $orderId = 42;
// The default kind is `SeeOther`, with the status 303. The browser loads// `/orders/42` with `GET`, so a reload does not send the form a second time.Core\Response::redirect('/orders/' . ($orderId as string));echo 'Status 303, Location: /orders/', $orderId, "\n";Status 303, Location: /orders/42