本文整理汇总了PHP中Tobscure\JsonApi\Document::addLink方法的典型用法代码示例。如果您正苦于以下问题:PHP Document::addLink方法的具体用法?PHP Document::addLink怎么用?PHP Document::addLink使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Tobscure\JsonApi\Document
的用法示例。
在下文中一共展示了Document::addLink方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: onKernelView
/**
* @param GetResponseForControllerResultEvent $event
*/
public function onKernelView(GetResponseForControllerResultEvent $event)
{
$request = $event->getRequest();
$document = $this->documentRepository->get($request->attributes->get('_definition'));
switch ($document->getDefinition()->info->version) {
case '2.0.0':
$result = $event->getControllerResult();
if (!$result instanceof Resource && !$result instanceof Document) {
$result = new Resource($result, $this->serializer);
}
if ($result instanceof Resource) {
$result = new Document($result);
}
if ($result instanceof Document) {
$result->addLink('self', $event->getRequest()->getPathInfo());
$event->setControllerResult($result->toArray());
}
break;
default:
//noop
}
}
示例2: addPaginationLinks
/**
* Add pagination links to a JSON-API response, based on input parameters
* and the default parameters of this action.
*
* @param Document $document
* @param JsonApiRequest $request
* @param string $url The base URL to build pagination links with.
* @param integer|boolean $total The total number of results (used to build
* a 'last' link), or just true if there are more results but how many
* is unknown ('last' link is ommitted).
* @return void
*/
protected function addPaginationLinks(Document $document, JsonApiRequest $request, $url, $total = true)
{
$input = [];
if ($request->limit != $this->limit) {
array_set($input, 'page.limit', $request->limit);
}
array_set($input, 'page.offset', 0);
$document->addLink('first', $url . '?' . http_build_query($input));
if ($request->offset > 0) {
array_set($input, 'page.offset', max(0, $request->offset - $request->limit));
$document->addLink('prev', $url . '?' . http_build_query($input));
}
if ($total === true || $request->offset + $request->limit < $total) {
array_set($input, 'page.offset', $request->offset + $request->limit);
$document->addLink('next', $url . '?' . http_build_query($input));
}
if ($total && $total !== true) {
array_set($input, 'page.offset', $total - $request->limit);
$document->addLink('last', $url . '?' . http_build_query($input));
}
}