CoreController.php 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. <?php
  2. /**
  3. * 核心控制器.
  4. *
  5. * @author nj <nj@gmail.com>
  6. * @date 2020-01-22 20:35:31
  7. */
  8. declare(strict_types=1);
  9. namespace App\Controller;
  10. use App\Constants\CommonConstant;
  11. use Hyperf\HttpServer\Contract\RequestInterface;
  12. use Hyperf\HttpServer\Contract\ResponseInterface;
  13. use Hyperf\HttpServer\Router\Router;
  14. use Psr\Container\ContainerInterface;
  15. use Hyperf\Di\Annotation\Inject;
  16. class CoreController
  17. {
  18. /**
  19. * 容器接口.
  20. *
  21. * @Inject
  22. * @var ContainerInterface
  23. */
  24. protected $container;
  25. /**
  26. * 请求接口.
  27. *
  28. * @Inject
  29. * @var RequestInterface
  30. */
  31. protected $request;
  32. /**
  33. * 相应接口.
  34. *
  35. * @Inject
  36. * @var ResponseInterface
  37. */
  38. protected $response;
  39. /**
  40. * 获取
  41. * @return array|mixed
  42. */
  43. protected function getJsonData()
  44. {
  45. return getDecodeData();
  46. }
  47. /**
  48. * 获取日志需要的当前模块名和动作名.
  49. * @return array
  50. */
  51. protected function getModuleAndAction()
  52. {
  53. $routeAction = $this->getCurrentRouteHandler();
  54. $controller_suffix = 'Controller';
  55. preg_match_all('/([a-z0-9A-Z]+\\\)?(\w+)@(\w+)/', $routeAction, $matchs);
  56. $controller_full = !empty($matchs[2][0]) ? $matchs[2][0] : ''; //类名.
  57. $controller = str_replace($controller_suffix, '', $controller_full); // 控制器名.
  58. $action = !empty($matchs[3][0]) ? $matchs[3][0] : ''; //方法名.
  59. return [$controller, $action];
  60. }
  61. /**
  62. * 获取当前路由话柄.
  63. * @return string
  64. */
  65. protected function getCurrentRouteHandler()
  66. {
  67. $currentUri = $this->request->getRequestUri();
  68. $currentMethod = $this->request->getMethod();
  69. $routeData = Router::getData();
  70. if (isset($routeData[0][$currentMethod][$currentUri])) {
  71. return $routeData[0][$currentMethod][$currentUri]->callback;
  72. } else {
  73. return '';
  74. }
  75. }
  76. }