/** * * @param clagiordano\weblibs\mvc\Application $application * @return clagiordano\weblibs\mvc\Controller */ public function __construct(Application $application) { $this->application = $application; $this->request = new Request(); $this->application->getRegistry()->requestType = $this->request->getType(); $this->application->getRegistry()->requestData = $this->request->getData(); }
/** * Trigger a request error (400) response (JSON format) * * @param unknown $code * @param string $msg The error message to return. * @param Request $request * @param unknown $data (optional) */ public function request_error($code, $msg, Request $request = null, $data = null) { header('HTTP/1.1 400'); Context::getInstance()->getLogger()->error(sprintf('Request error: %s - %s', $code, $msg)); if ($request && $request->getType() == 'JSONRPC') { $jsonError = array("jsonrpc" => "2.0", "error" => array("code" => $code, "msg" => $msg, "data" => $data), "id" => $request ? $request->getId() : null); if ($data === null) { array_pop($jsonError["error"]); } header('Content-Type: application/json'); echo json_encode($jsonError); } else { echo json_encode(array('error' => array('code' => $code, 'message' => $msg, 'data' => $data))); } ob_end_flush(); exit; }
/** * * сравнивает запрос с роутами. результат - контроллер, экшн, параметры * @param Request $request * @return array */ public function match(Request $request) { $method = strtolower($request->getType()); $uri = $request->getURI(); $container = '_routes_' . $method; $result = array('controller' => $this->_controller, 'action' => $this->_action); foreach ($this->{$container} as $route => $controller) { if (preg_match($route, $uri, $matches)) { $result['controller'] = $this->_controller = explode('@', $controller)[0]; $result['action'] = explode('@', $controller)[1]; foreach ($matches as $key => $match) { if (is_string($key)) { $result[$key] = $match; } } break; } } return $result; }