* @date 2020-01-22 20:35:31 */ declare(strict_types=1); namespace App\Controller; use App\Constants\CommonConstant; use Hyperf\HttpServer\Contract\RequestInterface; use Hyperf\HttpServer\Contract\ResponseInterface; use Hyperf\HttpServer\Router\Router; use Psr\Container\ContainerInterface; use Hyperf\Di\Annotation\Inject; class CoreController { /** * 容器接口. * * @Inject * @var ContainerInterface */ protected $container; /** * 请求接口. * * @Inject * @var RequestInterface */ protected $request; /** * 相应接口. * * @Inject * @var ResponseInterface */ protected $response; /** * 获取 * @return array|mixed */ protected function getJsonData() { return getDecodeData(); } /** * 获取日志需要的当前模块名和动作名. * @return array */ protected function getModuleAndAction() { $routeAction = $this->getCurrentRouteHandler(); $controller_suffix = 'Controller'; preg_match_all('/([a-z0-9A-Z]+\\\)?(\w+)@(\w+)/', $routeAction, $matchs); $controller_full = !empty($matchs[2][0]) ? $matchs[2][0] : ''; //类名. $controller = str_replace($controller_suffix, '', $controller_full); // 控制器名. $action = !empty($matchs[3][0]) ? $matchs[3][0] : ''; //方法名. return [$controller, $action]; } /** * 获取当前路由话柄. * @return string */ protected function getCurrentRouteHandler() { $currentUri = $this->request->getRequestUri(); $currentMethod = $this->request->getMethod(); $routeData = Router::getData(); if (isset($routeData[0][$currentMethod][$currentUri])) { return $routeData[0][$currentMethod][$currentUri]->callback; } else { return ''; } } }