public function send() { if (\false === $this->hasHeader('Content-Type')) { $this->addHeader('Content-Type', 'text/html; charset=utf8'); } return parent::send(); }
public function send() { $this->removeHeader('Content-Type'); $this->addHeader('Content-Type', 'application/json'); $this->setBody(json_encode($this->body)); return parent::send(); }
/** * Create an HTML response. * * Produces an HTML response with a Content-Type of text/html and a default * status of 200. * * @param string|StreamInterface $html HTML or stream for the message body. * @param int $status Integer status code for the response; 200 by default. * @param array $headers Array of headers to use at initialization. * @throws InvalidArgumentException if $html is neither a string or stream. */ public function __construct($html = '', $status = 200, array $headers = []) { parent::__construct($this->createBody($html), $status, []); $headers = $headers + ['Content-Type' => 'text/html; charset=utf-8']; foreach ($headers as $name => $value) { $this->withHeader($name, $value); } }
/** * Create a JSON response with the given data. * * Default JSON encoding is performed with the following options, which * produces RFC4627-compliant JSON, capable of embedding into HTML. * * - JSON_HEX_TAG * - JSON_HEX_APOS * - JSON_HEX_AMP * - JSON_HEX_QUOT * - JSON_UNESCAPED_SLASHES * * @param mixed $data Data to convert to JSON. * @param int $status Integer status code for the response; 200 by default. * @param array $headers Array of headers to use at initialization. * @param int $encodingOptions JSON encoding options to use. * @throws InvalidArgumentException if unable to encode the $data to JSON. */ public function __construct($data = '', $status = 200, array $headers = [], $encodingOptions = self::DEFAULT_JSON_FLAGS) { $body = new Stream(fopen('php://temp', 'r+')); $body->write($this->jsonEncode($data, $encodingOptions)); $body->rewind(); parent::__construct($body, $status, []); $headers = $headers + ['Content-Type' => 'application/json']; foreach ($headers as $name => $value) { $this->withHeader($name, $value); } }
/** * Create a redirect response. * * Produces a redirect response with a Location header and the given status * (302 by default). * * Note: this method overwrites the `location` $headers value. * * @param string|UriInterface $uri URI for the Location header. * @param int $status Integer status code for the redirect; 302 by default. * @param array $headers Array of headers to use at initialization. */ public function __construct($uri, $status = 302, array $headers = []) { if (!is_string($uri) && !$uri instanceof UriInterface) { throw new InvalidArgumentException(sprintf('Uri provided to %s MUST be a string or Psr\\Http\\Message\\UriInterface instance; received "%s"', __CLASS__, is_object($uri) ? get_class($uri) : gettype($uri))); } parent::__construct(\null, $status, []); $headers = ['Location' => (string) $uri] + $headers; foreach ($headers as $name => $value) { $this->withHeader($name, $value); } }
public function __construct($url, $code = 302) { parent::__construct('', $code); $this->setUrl($url); }
/** * @param string $package * @param Http\Request $request * @param Http\Response $response * @param bool $subRequest * @throws CoreException * @return \Micro\Http\Response */ public function resolve($package, Http\Request $request, Http\Response $response, $subRequest = \false) { if (!is_string($package) || strpos($package, '@') === \false) { throw new CoreException('[' . __METHOD__ . '] Package must be in [Package\\Handler@action] format', 500); } list($package, $action) = explode('@', $package); if (!class_exists($package, \true)) { throw new CoreException('[' . __METHOD__ . '] Package class "' . $package . '" not found', 404); } $parts = explode('\\', $package); $packageParam = Utils::decamelize($parts[0]); $controllerParam = Utils::decamelize($parts[count($parts) - 1]); $actionParam = Utils::decamelize($action); $request->setParam('package', $packageParam); $request->setParam('controller', $controllerParam); $request->setParam('action', $actionParam); $packageInstance = new $package($request, $response); if ($packageInstance instanceof Controller) { $actionMethod = lcfirst(Utils::camelize($action)) . 'Action'; } else { $actionMethod = lcfirst(Utils::camelize($action)); } if (!method_exists($packageInstance, $actionMethod)) { throw new CoreException('[' . __METHOD__ . '] Method "' . $actionMethod . '" not found in "' . $package . '"', 404); } if ($packageInstance instanceof ContainerAwareInterface) { $packageInstance->setContainer($this); } $scope = ''; if ($packageInstance instanceof Controller) { $packageInstance->init(); $scope = $packageInstance->getScope(); } if (($packageResponse = $packageInstance->{$actionMethod}()) instanceof Http\Response) { return $packageResponse; } if (is_object($packageResponse) && !$packageResponse instanceof View) { throw new CoreException('[' . __METHOD__ . '] Package response is object and must be instance of View', 500); } if ($packageResponse === \null || is_array($packageResponse)) { if ($packageInstance instanceof Controller) { $view = $packageInstance->getView(); } else { $view = new View(); } if (is_array($packageResponse)) { $view->addData($packageResponse); } $packageResponse = $view; } if ($packageResponse instanceof View) { if ($packageResponse->getTemplate() === \null) { $packageResponse->setTemplate(($scope ? $scope . '/' : '') . $controllerParam . '/' . $actionParam); } $packageResponse->injectPaths((array) package_path($parts[0], 'Resources/views')); if (($eventResponse = $this->get('event')->trigger('render.start', ['view' => $packageResponse])) instanceof Http\Response) { return $eventResponse; } if ($subRequest) { $packageResponse->setRenderParent(\false); } $response->setBody((string) $packageResponse->render()); } else { $response->setBody((string) $packageResponse); } return $response; }
/** * Create an empty response with the given status code. * * @param int $status Status code for the response, if any. * @param array $headers Headers for the response, if any. */ public function __construct($status = 204, array $headers = []) { parent::__construct(\null, $status, $headers); }