phroute icon indicating copy to clipboard operation
phroute copied to clipboard

Back route

Open brunocascio opened this issue 9 years ago • 2 comments

Possibility to add back() function?

brunocascio avatar Oct 03 '15 23:10 brunocascio

Thanks for the suggestion... What would the function do? Do you have a use case?

mrjgreen avatar Dec 27 '15 23:12 mrjgreen

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.']);
  }

  [...]
}

....

brunocascio avatar Dec 28 '15 11:12 brunocascio