本文整理汇总了PHP中Psr\Http\Message\ResponseInterface::withHeader方法的典型用法代码示例。如果您正苦于以下问题:PHP ResponseInterface::withHeader方法的具体用法?PHP ResponseInterface::withHeader怎么用?PHP ResponseInterface::withHeader使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Psr\Http\Message\ResponseInterface
的用法示例。
在下文中一共展示了ResponseInterface::withHeader方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: setHeadersFromArray
/**
* Add headers represented by an array of header lines.
*
* @param string[] $headers Response headers as array of header lines.
*
* @return $this
*
* @throws \UnexpectedValueException For invalid header values.
* @throws \InvalidArgumentException For invalid status code arguments.
*/
public function setHeadersFromArray(array $headers)
{
$statusLine = trim(array_shift($headers));
$parts = explode(' ', $statusLine, 3);
if (count($parts) < 2 || substr(strtolower($parts[0]), 0, 5) !== 'http/') {
throw new \UnexpectedValueException(sprintf('"%s" is not a valid HTTP status line', $statusLine));
}
$reasonPhrase = count($parts) > 2 ? $parts[2] : '';
$this->response = $this->response->withStatus((int) $parts[1], $reasonPhrase)->withProtocolVersion(substr($parts[0], 5));
foreach ($headers as $headerLine) {
$headerLine = trim($headerLine);
if ('' === $headerLine) {
continue;
}
$parts = explode(':', $headerLine, 2);
if (count($parts) !== 2) {
throw new \UnexpectedValueException(sprintf('"%s" is not a valid HTTP header line', $headerLine));
}
$name = trim(urldecode($parts[0]));
$value = trim(urldecode($parts[1]));
if ($this->response->hasHeader($name)) {
$this->response = $this->response->withAddedHeader($name, $value);
} else {
$this->response = $this->response->withHeader($name, $value);
}
}
return $this;
}
示例2: htmlBody
protected function htmlBody(array $data)
{
// Aura.view setup
$view_factory = new ViewFactory();
$view = $view_factory->newInstance();
// add templates to the view registry
$view_registry = $view->getViewRegistry();
// main view
$layout_registry = $view->getLayoutRegistry();
$layout_registry->set('layout', $this->viewDir . '/layout.php');
// partial view
$view_registry = $view->getViewRegistry();
$partial = $this->request->getAttribute('_content');
$view_registry->set('_content', $this->viewDir . '/_' . $partial);
// set partial view main content file as dynamic partial
$dataset = ['data' => $data, 'partial' => $partial];
$view->setData($dataset);
// do it
// set views
$view->setView('_content');
$view->setLayout('layout');
$output = $view->__invoke();
// or just $view()
//
$this->response = $this->response->withHeader('Content-Type', 'text/html');
$this->response->getBody()->write($output);
}
示例3: execute
public function execute(Request $request, Response $response, callable $next = null)
{
if ($this->boot()->auth->getIdentity()) {
return $response->withHeader('Location', "/");
}
return $next($request, $response);
}
示例4: __invoke
public function __invoke(Request $request, Response $response, callable $next = null)
{
if (Auth::guest()) {
return $response->withHeader('Location', '/auth/login');
}
return $next($request, $response);
}
示例5: execute
public function execute(Request $req, Response $res, callable $next = null)
{
try {
$id = $req->getAttribute('id');
//$name = urldecode($req->getAttribute('name'));
//$params = $req->getQueryParams();
$debtentrosumma = $this->boot()->get('debtentrosumma');
$entrodebtsumma = $this->boot()->get('entrodebtsumma');
$data1 = $debtentrosumma->byId($id)->current();
$data2 = $entrodebtsumma->byId($id)->current();
$s1 = is_array($data1) ? $data1['summa'] : 0;
$s2 = is_array($data2) ? $data2['summa'] : 0;
if ($s1 == $s2) {
$s1 = 0;
$s2 = 0;
} else {
$s1 = $s1 > $s2 ? $s1 - $s2 : 0;
$s2 = $s2 > $s1 ? $s2 - $s1 : 0;
}
$html = "<table border='1'><tr><th colspan='2'>Деньги</th></tr>";
$html .= "<tr><th>мы должны</th><th>субъект должен нам</th></tr>";
$html .= "<tr><td>{$s2}</td><td>{$s1}</td></tr></table>";
return $res->withHeader('Content-Type', 'text/html')->withBody($this->toStream($html));
} catch (\Exception $ex) {
return new JsonResponse(['status' => 'error', 'error' => $ex->getMessage()], 500);
}
}
示例6: checkRememberMe
/**
* Check Remember Me
*
* This method checks whether RememberMe cookie exists
* If it does, get the user credentials and make it global for easy use
* If not, just route the login page
*
* @param ServerRequestInterface $req PSR-7 Request
* @param ResponseInterface $res PSR-7 Response
*
* @return callable
*/
protected function checkRememberMe(Request $req, Response $res)
{
dd(Cookies::get('hihi'));
die;
$cookie = $this->app->cookies->get($this->c['myConfig']->get('auth.remember'));
if ($cookie && !$this->app->auth) {
$credentials = explode('___', $cookie);
// If the cookie isn't valid
if (empty(trim($cookie)) || count($credentials) !== 2) {
return $res->withHeader('Location', $this->app->router->pathFor('login'));
}
$identifier = $credentials[0];
$hashLib = $this->c->get('hash');
$token = $hashLib->hash($credentials[1]);
$user = User::where('remember_identifier', $identifier)->first();
if ($user) {
if ($hashLib->hashCheck($token, $user->remember_token)) {
// Finally, user can login
$_SESSION[$this->c['myConfig']->get('auth.session')] = $user->user_id;
$this->app->auth = $user;
} else {
$user->removeRememberCredentials();
}
}
// Endif user is found in DB
}
// Endif the cookie is there
}
示例7: execute
public function execute(Request $request, Response $response, callable $next = null)
{
$collectionname = $request->getAttribute('collectionname');
$collectionname = $this->boot()->config['collections'][$collectionname];
$collection = $this->boot()->get($collectionname);
return $response->withHeader('Content-Type', 'application/json')->withBody($this->toJsonStream([['name' => $collection], ['name' => 'sdfgds4567657 56fgsdfg'], ['name' => 'sdfgdsfgsdfg 56 756745670']]));
}
示例8: modify
public function modify(ResponseInterface $response) : ResponseInterface
{
if (false === strpos($response->getHeaderLine('Content-Type'), 'application/json')) {
return $response;
}
return $response->withHeader('Content-Type', $this->contentType);
}
示例9: render
/**
*
* @param ResponseInterface $response
* @param int $statusCode
* @param array $data
*
* @return ResponseInterface
*
* @throws \InvalidArgumentException
* @throws \RuntimeException
*/
public function render(ResponseInterface $response, $statusCode = 200, array $data = [])
{
$newResponse = $response->withHeader('Content-Type', 'application/json');
$newResponse = $newResponse->withStatus($statusCode);
$newResponse->getBody()->write(json_encode($data));
return $newResponse;
}
示例10: htmlBody
protected function htmlBody($data)
{
if (isset($data)) {
$this->response = $this->response->withHeader('Content-Type', 'text/html');
$this->response->getBody()->write($data);
}
}
示例11: __invoke
/**
* Execute the middleware.
*
* @param RequestInterface $request
* @param ResponseInterface $response
* @param callable $next
*
* @return ResponseInterface
*/
public function __invoke(RequestInterface $request, ResponseInterface $response, callable $next)
{
if ($request->getUri()->getPath() === '/robots.txt') {
$body = Middleware::createStream();
if ($this->allow) {
$body->write("User-Agent: *\nAllow: /");
} else {
$body->write("User-Agent: *\nDisallow: /");
}
return $next($request, $response->withBody($body));
}
if ($this->allow) {
return $next($request, $response->withHeader(self::HEADER, 'index, follow'));
}
return $next($request, $response->withHeader(self::HEADER, 'noindex, nofollow, noarchive'));
}
示例12: __invoke
public function __invoke(ServerRequestInterface $request, ResponseInterface $response, callable $out = null)
{
$page = intval($request->getAttribute('page')) ?: 0;
$pictures = $this->apodApi->getPage($page, $this->resultsPerPage);
$response->getBody()->write(json_encode($pictures));
return $response->withHeader('Cache-Control', ['public', 'max-age=3600'])->withHeader('Content-Type', 'application/json');
}
示例13: __invoke
public function __invoke(Request $request, Response $response, callable $next = null)
{
if (!$this->boot()->get('auth')->getIdentity()) {
return $response->withHeader('Location', "/auth");
}
return $this->execute($request, $response, $next);
}
示例14: exec
/**
* Execute the router.
*
* @return ResponseInterface
* @throws AppException
* @throws RuntimeException
* @throws UnexpectedValueException
*/
public function exec()
{
global $CONFIG;
$this->response = $this->response->withHeader('Content-Type', $this->getOutputContentType());
$routeMethod = $this->route->getMethod();
$requestMethod = strtolower($this->request->getMethod());
// OPTIONS request, don't response
if ($requestMethod == 'options') {
return $this->response;
}
$mode = 0;
//error_log('[exec] >>>>>>>'.$routeMethod.'####'.$requestMethod);
if (!is_null($routeMethod) && $routeMethod != $requestMethod) {
if ($CONFIG->strict) {
throw new AppException('Request method does not match the route request method.');
} else {
error_log(sprintf('[WARING] Request method `%s` does not match the route request method `%s`.', $requestMethod, $routeMethod));
}
} else {
$mode = $requestMethod == 'get' ? 1 : 2;
}
if ($this->hasAction()) {
$params = $this->getFilteredParams($mode);
if (count($this->invalid)) {
error_log('Invalid inputs: ' . var_export($this->invalid, true));
$invalidKeys = array_map(function ($item) {
return $item['name'] . ' - ' . $item['code'];
}, $this->invalid);
throw new RuntimeException('Invalid inputs : ' . implode(',', array_values($invalidKeys)));
}
$context = new Context($this->request);
$params = array_merge(array('_CONTEXT_' => $context), $params);
//$result = call_user_func_array(array($this->controller, $this->getNormalizedAction()), $params);
$closure = $this->controllerClosure;
$result = $closure($this->getNormalizedAction(), $params);
error_log('###############' . var_export($result, true));
if ($CONFIG->strict && (is_null($result) || !is_numeric($result))) {
throw new UnexpectedValueException('Controller must return numeric value.');
}
$statusMessage = $this->controller->getStatusMessage();
if (!is_null($result)) {
$statusCode = $result;
} else {
$statusCode = $this->controller->getStatusCode();
if (is_null($result)) {
$statusCode = Constants::SYS_SUCCESS;
error_log('[WARNING] ' . $this->controllerClassName . ' ' . $this->getNormalizedAction() . ' has not return code, use default return code.');
}
}
$outputs = $this->controller->getOutputs();
$json = array('status' => (int) $statusCode, 'msg' => (string) $statusMessage);
if (!empty($outputs)) {
$json['data'] = $outputs;
}
return $this->processResponse($json, $this->response);
} else {
throw new AppException(sprintf('Action %s not found in Controller %s', $this->getNormalizedAction(), $this->getControllerClassName()));
}
}
示例15: withHeader
/**
* Proxy to PsrResponseInterface::withHeader()
*
* {@inheritdoc}
*/
public function withHeader($header, $value)
{
if ($this->complete) {
return $this;
}
$new = $this->psrResponse->withHeader($header, $value);
return new self($new);
}