本文整理汇总了PHP中Psr\Http\Message\RequestInterface::getParsedBody方法的典型用法代码示例。如果您正苦于以下问题:PHP RequestInterface::getParsedBody方法的具体用法?PHP RequestInterface::getParsedBody怎么用?PHP RequestInterface::getParsedBody使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Psr\Http\Message\RequestInterface
的用法示例。
在下文中一共展示了RequestInterface::getParsedBody方法的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: __invoke
/**
* Invoke middleware
*
* @param RequestInterface $request PSR7 request object
* @param ResponseInterface $response PSR7 response object
* @param callable $next Next middleware callable
*
* @return ResponseInterface PSR7 response object
*/
public function __invoke(RequestInterface $request, ResponseInterface $response, callable $next)
{
// Validate POST, PUT, DELETE, PATCH requests
if (in_array($request->getMethod(), ['POST', 'PUT', 'DELETE', 'PATCH'])) {
$body = $request->getParsedBody();
$body = $body ? (array) $body : [];
$name = isset($body[$this->prefix . '_name']) ? $body[$this->prefix . '_name'] : false;
$value = isset($body[$this->prefix . '_value']) ? $body[$this->prefix . '_value'] : false;
if (!$name || !$value || !$this->validateToken($name, $value)) {
return $response->withStatus(400);
}
}
// Generate new CSRF token
$name = $this->prefix . mt_rand(0, mt_getrandmax());
$value = $this->createToken();
$this->saveToStorage($name, $value);
$request = $request->withAttribute($this->prefix . '_name', $name)->withAttribute($this->prefix . '_value', $value);
return $next($request, $response);
}
示例2: run
/**
* Action logic
*
* @param RequestInterface $request Request
* @param ResponseInterface $response Response
* @return ResponseInterface
*/
public function run(RequestInterface $request, ResponseInterface $response)
{
/** @var \Psr\Http\Message\ServerRequestInterface $request */
/** @var \Doctrine\ORM\EntityManager $entityManager */
// recovering the user data and validate
$user = $this->getUser($request->getAttribute('id'));
$validator = new UserValidator('modify', $request, $this->container);
if (!$validator->validate()) {
return new ValidationJsonResponse($validator->errors());
}
// update the user
$entityManager = $this->container->get('EntityManager');
$data = $request->getParsedBody();
foreach ($data as $field => $value) {
$user->{'set' . ucfirst($field)}($value);
}
$entityManager->flush();
return new JsonResponse(['status' => 'updated'], 200);
}
示例3: run
/**
* Action logic
*
* @param RequestInterface $request Petición
* @param ResponseInterface $response Respuesta
* @return ResponseInterface
*/
public function run(RequestInterface $request, ResponseInterface $response)
{
/** @var \Psr\Http\Message\ServerRequestInterface $request */
/** @var \Doctrine\ORM\EntityManager $entityManager */
// data is validated
$validator = new UserValidator('new', $request, $this->container);
if (!$validator->validate()) {
return new ValidationJsonResponse($validator->errors());
}
// insert the user
$entityManager = $this->container->get('EntityManager');
$data = $request->getParsedBody();
$user = new User();
foreach ($data as $field => $value) {
$user->{'set' . ucfirst($field)}($value);
}
$entityManager->persist($user);
$entityManager->flush();
return new JsonResponse(['status' => 'created'], 201);
}
示例4: __invoke
/**
* Invoke middleware
*
* @param RequestInterface $request PSR7 request object
* @param ResponseInterface $response PSR7 response object
* @param callable $next Next middleware callable
*
* @return ResponseInterface PSR7 response object
*/
public function __invoke(RequestInterface $request, ResponseInterface $response, callable $next)
{
// Validate POST, PUT, DELETE, PATCH requests
if (in_array($request->getMethod(), ['POST', 'PUT', 'DELETE', 'PATCH'])) {
$body = $request->getParsedBody();
$body = $body ? (array) $body : [];
$name = isset($body[$this->prefix . '_name']) ? $body[$this->prefix . '_name'] : false;
$value = isset($body[$this->prefix . '_value']) ? $body[$this->prefix . '_value'] : false;
if (!$name || !$value || !$this->validateToken($name, $value)) {
// Need to regenerate a new token, as the validateToken removed the current one.
$request = $this->generateNewToken($request);
$failureCallable = $this->getFailureCallable();
return $failureCallable($request, $response, $next);
}
}
// Generate new CSRF token
$request = $this->generateNewToken($request);
// Enforce the storage limit
$this->enforceStorageLimit();
return $next($request, $response);
}
示例5: deleteQueue
public function deleteQueue(Request $request, Response $response, array $args)
{
global $database;
$err = new \Malmanger\Mpmq\Util\ErrorHandler();
$data = $request->getParsedBody();
$this->log->debug("deleteQueue args=" . print_r($args, true));
$queue = new DbQueue($args['id']);
if (!$queue->queueExists()) {
$err->addNotFound($args['id']);
}
if (!$queue->delete()) {
$err->addDbUpdate("deleteQueue");
}
if ($err->getLevel() > 0) {
return $err->getErrorResponse($response);
} else {
$resp = new \Malmanger\Mpmq\Util\ResponseHandler();
return $resp->setStatus(204, 'Queue deleted')->getResponse($response);
}
}
示例6: newMessage
/**
* Add new message to queue
*
* @param Psr\Http\Message\RequestInterface $request
* @param Psr\Http\Message\ResponseInterface $response
* @param array $args
*
* @return Psr\Http\Message\ResponseInterface
*/
public function newMessage(Request $request, Response $response, array $args)
{
global $database;
$err = new \Malmanger\Mpmq\Util\ErrorHandler();
$data = $request->getParsedBody();
$this->log->debug("newMessage data=" . print_r($data, true));
// Check for mandatory parameters and set defaults
$id = $args['id'];
$queue = new DbQueue($id);
if (!$queue->queueExists()) {
$err->addNotFound("queue with id=" . $id);
}
$messageId = null;
$title = null;
$payload = null;
$timeout = null;
// $key = "id";
// if (!array_key_exists($key, $data)) {
// $err->addMissing($key);
// } else {
// $id = $data["id"];
// }
$key = "title";
if (!array_key_exists($key, $data)) {
$err->addMissing($key);
} else {
$title = $data[$key];
}
$key = "payload";
if (array_key_exists($key, $data)) {
$payload = $data[$key];
}
$key = "timeout";
if (array_key_exists($key, $data)) {
$timeout = $data[$key];
} else {
$timeout = $queue->getTimeout();
}
$message = new DbMessage($id, $messageId, $title, $payload, $timeout);
// if ($queue->queueExists()) {
// $err->addExists($id);
// }
if ($err->getLevel() > 0) {
return $err->getErrorResponse($response);
}
$messageId = $message->save();
if (!$messageId) {
$err->addDbUpdate("newMessage");
}
if ($err->getLevel() > 0) {
return $err->getErrorResponse($response);
} else {
$data = array();
$data['id'] = $message->getQueueId();
$data['messageId'] = $message->getMessageId();
$data['title'] = $message->getTitle();
$data['payload'] = $message->getPayload();
$data['timeout'] = $message->getTimeout();
$data['inFlight'] = $message->getInFlight();
$resp = new \Malmanger\Mpmq\Util\ResponseHandler($data);
return $resp->getResponse($response);
}
}