Index.php 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. <?php
  2. namespace EasySwoole\Http\Tests\ControllerWithRouter;
  3. use EasySwoole\Component\Context\ContextManager;
  4. use EasySwoole\Http\AbstractInterface\Controller;
  5. class Index extends Controller
  6. {
  7. protected $handler = false;
  8. public function index()
  9. {
  10. $this->response()->write('index');
  11. }
  12. public function user()
  13. {
  14. $this->response()->write(json_encode([
  15. 'get' => $this->request()->getQueryParams(),
  16. 'post' => $this->request()->getParsedBody(),
  17. 'context' => ContextManager::getInstance()->get(Router::PARSE_PARAMS_CONTEXT_KEY)
  18. ]));
  19. ContextManager::getInstance()->destroyAll();
  20. }
  21. public function exception()
  22. {
  23. throw new \Exception('the error');
  24. }
  25. public function httpExceptionHandler()
  26. {
  27. $this->handler = true;
  28. throw new \Exception('the handler');
  29. }
  30. public function actionNotFound(?string $action)
  31. {
  32. return $this->response()->withStatus(404)->write("{$action}-404");
  33. }
  34. protected function onException(\Throwable $throwable): void
  35. {
  36. if ($this->handler === true) throw $throwable;
  37. $this->response()->withStatus(500)->write("error-{$throwable->getMessage()}");
  38. }
  39. }