phroute
phroute copied to clipboard
Back route
Possibility to add back()
function?
Thanks for the suggestion... What would the function do? Do you have a use case?
You could use these function when should redirect for example in validation errors (talking of MVC) or when the operations are successful.
private static function dispatchRedirect($uri){
header("Location: $uri");
exit;
}
public static function back($status = 302, $headers = []) {
$uri = $_SERVER['HTTP_REFERER'];
self::dispatchRedirect($uri, $status, $headers);
}
Fake example code :
...
public function exampleActionOfController(){
$data = $_POST;
$valid = validate($data);
if (!$valid) {
$router->back();
// In the real life, use this with session flash messages
// $router->back()->with(['errors' => 'Cannot save the object.']);
}
[...]
}
....