PHP MVC: How to quickly exit Actions / Controllers?

In the PHP MVC framework, how can I cleanly and elegantly exit the current controller / action, but continue to execute the script

For example, suppose my framework usually follows this outline:

  • Map URL for controller / action
  • Instant controller control, action call (capture output)
    • Make stuff
    • Render view
    • At the end of the method of continuing normal operation
  • Process exit if necessary
  • Send output to browser

Now let's say that I want to stop the “normal” execution somewhere in the “Do” step in order to, say, display a different view or redirect the title, and I want to stop processing the rest of the body Action, but continue on the “Exit the process” step

How can I achieve this in the best way? My only ideas:

//in controller
protected function redirect($url) {
    header("Location: $url");
    exit();
}

but this completely skips the rest of the wireframe execution and flushes everything that was in the output buffer directly to the user. Alternative:

//in dispatcher
call_user_func_array(array($controller,$action),$params);
afterwards:
...

//in controller
protected function redirect($url) {
    header("Location: $url");
    goto afterwards;
}

However, this makes me twitch and runs counter to everything that I have learned, especially because the label to which it refers is in another file completely.

So, is there any other way to achieve this?

. , , exit(), . .

+5
3

Action , . . , , , .

+3

, " " "? ... .

+2

. ! , . . . , . . , .

public function view(int $user_id): ResponseHandler {
  $this->validate($user_id); //throws exception

  if (1 == 2) throw new ControllerInvalidArgumentException();

  $view = new View('nameof view');

  return new ResponseHandler($view);

}

, , laravel symfony. , . , , , , .

0

All Articles