Does anybody know how to avoid the brutal PHP built-in exit(); function and use the "right" Drupal 8 class method to terminate the request? If so: a bit explanation would be appreciated.

  • 1
    Where are you trying to do this? Why would you want to return a json response anywhere else than in a controller, where you can just return it? – Berdir Sep 8 '16 at 22:52

You would have been looking for drupal_exit() in the past, but this was removed in D8. It is now advised to use the appropriate HTTP Exception. See the example on the change notice:


  • That is not what I'm looking for. I'm sending a response via $response = new JsonResponse(); $response->setData(array( 'data' => 123 )); $response->send(); Afterwards I do not want, that Drupal does any data output. But there might be any "terminate" events which should be triggered. With exit(); they are just killed and this is not good. – jepster Sep 8 '16 at 15:38
  • You might want to reference this: stackoverflow.com/questions/31584341/… – Kevin Sep 8 '16 at 15:44

Not the answer you're looking for? Browse other questions tagged or ask your own question.