| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687 |
- <?php
- /**
- * 核心控制器.
- *
- * @author nj <nj@gmail.com>
- * @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
- {
- use \Hyperf\Di\Aop\ProxyTrait;
- use \Hyperf\Di\Aop\PropertyHandlerTrait;
- function __construct()
- {
- $this->__handlePropertyHandler(__CLASS__);
- }
- /**
- * 容器接口.
- *
- * @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 '';
- }
- }
- }
|