本文整理汇总了PHP中Slim\Http\Response::setBody方法的典型用法代码示例。如果您正苦于以下问题:PHP Response::setBody方法的具体用法?PHP Response::setBody怎么用?PHP Response::setBody使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Slim\Http\Response
的用法示例。
在下文中一共展示了Response::setBody方法的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: write
/**
* This function outputs the given $data as valid HAL+JSON to the client
* and sets the HTTP Response Code to the given $statusCode.
*
* @param array|SlimBootstrap\DataObject $data The data to output to
* the client
* @param int $statusCode The status code to set
* in the reponse
*/
public function write($data, $statusCode = 200)
{
$path = $this->_request->getPath();
$hal = new hal\Hal($path);
if (true === is_array($data)) {
$pathData = explode('/', $path);
unset($pathData[0]);
$endpointName = end($pathData);
$endpointUri = '/' . implode('/', $pathData) . '/';
foreach ($data as $entry) {
/** @var SlimBootstrap\DataObject $entry */
$identifiers = $entry->getIdentifiers();
$resourceName = $endpointUri . implode('/', array_values($identifiers));
$resource = new hal\Hal($resourceName, $entry->getData() + $entry->getIdentifiers());
$this->_addAdditionalLinks($resource, $entry->getLinks());
$hal->addLink($endpointName, $resourceName);
$hal->addResource($endpointName, $resource);
}
} else {
$hal->setData($data->getData() + $data->getIdentifiers());
$this->_addAdditionalLinks($hal, $data->getLinks());
}
$body = $this->_jsonEncode($hal);
if (false === $body) {
$this->_response->setStatus(500);
$this->_response->setBody("Error encoding requested data.");
return;
}
$this->_headers->set('Content-Type', 'application/hal+json; charset=UTF-8');
$this->_response->setStatus($statusCode);
$this->_response->setBody($hal->asJson());
}
示例2: setResult
public function setResult(RestApi_Result $result)
{
foreach ($result->getHeaders() as $name => $value) {
$this->response->headers()->set($name, $value);
}
$this->response->setStatus($result->getCode());
$this->response->setBody($result->getBody());
}
示例3: __invoke
public function __invoke()
{
$root = __DIR__ . '/..';
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
$soldierIdentity = new SoldierIdentity($_POST['firstName'], $_POST['lastName'], $_POST['nickName'], (int) $_POST['nationality']);
$soldierServiceRecord = new SoldierServiceRecord((int) $_POST['status'], (int) $_POST['rank'], (int) $_POST['specialization'], (int) $_POST['numMissions'], (int) $_POST['numKills'], (int) $_POST['hp'], (int) $_POST['will'], (int) $_POST['defence'], (int) $_POST['aim']);
$soldier = new Soldier($soldierIdentity, $soldierServiceRecord);
$persister = new SoldierPersister((new Sqlite3Factory($root . '/db/data.sqlite3'))->db());
$persister->saveNew($soldier);
header('303 See Other');
header('Location: /');
} else {
$this->response->setBody((new TemplateFactory([$root . '/tpl']))->loadTemplate('create.twig')->render([]));
}
}
示例4: __invoke
public function __invoke()
{
$root = __DIR__ . '/..';
$db = new Sqlite3($root . '/db/data.sqlite3', SQLITE3_OPEN_READWRITE);
$openErr = $db->lastErrorCode();
if ($openErr) {
throw new Exception($db->lastErrorMsg());
}
$db->enableExceptions(true);
$stmt = $db->prepare(self::QUERY_SOLDIERS_LIST);
$result = $stmt->execute();
/**
* @var $soldierList Soldier[]
*/
$soldierList = [];
while ($record = $result->fetchArray(SQLITE3_ASSOC)) {
$soldierIdentity = new SoldierIdentity($record['firstName'], $record['lastName'], $record['nickName'], $record['nationality']);
$soliderServiceRecord = new SoldierServiceRecord($record['status'], $record['rank'], $record['specialization'], $record['numMissions'], $record['numKills'], $record['hp'], $record['will'], $record['defence'], $record['aim']);
$soldierList[] = new Soldier($soldierIdentity, $soliderServiceRecord);
}
$this->response->setBody((new TemplateFactory([$root . '/tpl']))->loadTemplate('home.twig')->render(['soldierList' => $soldierList]));
}
示例5: write
/**
* This function outputs the given $data as valid JSON to the client
* and sets the HTTP Response Code to the given $statusCode.
*
* @param array|SlimBootstrap\DataObject $data The data to output to
* the client
* @param int $statusCode The status code to set
* in the response
*/
public function write($data, $statusCode = 200)
{
$result = array();
if (true === is_array($data)) {
foreach ($data as $entry) {
/** @var SlimBootstrap\DataObject $entry */
$identifiers = array_values($entry->getIdentifiers());
$this->_buildStructure($entry, $identifiers, 0, $result);
}
} else {
$identifiers = array_values($data->getIdentifiers());
$this->_buildStructure($data, $identifiers, 0, $result);
}
$body = $this->_jsonEncode($result);
if (false === $body) {
$this->_response->setStatus(500);
$this->_response->setBody("Error encoding requested data.");
return;
}
$this->_headers->set('Content-Type', 'application/json; charset=UTF-8');
$this->_response->setStatus($statusCode);
$this->_response->setBody($body);
}
示例6: write
/**
* This function outputs the given $data as valid CSV to the client
* and sets the HTTP Response Code to the given $statusCode.
*
* @param SlimBootstrap\DataObject[] $data The data to output to
* the client
* @param int $statusCode The status code to set
* in the response
* @throws CSVEncodingException
*/
public function write($data, $statusCode = 200)
{
if ($data instanceof DataObject) {
$data = array($data);
} else {
if (false === \is_array($data)) {
throw new CSVEncodingException('Expected array of DataObjects or one DataObject, but ' . \gettype($data) . ' given.');
}
}
$body = $this->_csvEncode($data, $this->_encloseAll);
$this->_headers->set('Content-Type', 'text/csv; charset=UTF-8');
$this->_response->setStatus($statusCode);
$this->_response->setBody($body);
}