Symfony REST分页

时间:2018-07-01 19:34:36

标签: rest symfony pagination

我有以下问题,我想进行分页,但没有在纯symfony中使用FosRestBundle。问题在于分页理论上可行,但实际上json仅返回当前页面,限制和没有数据的页面数。我的代码:

$doctrine = $this->getDoctrine();
$limit = $request->get('limit', 2);
$page = $request->get('page', 1);
$offset = ($page -1) *$limit;

$nameRepository = $doctrine->getRepository('AppBundle:Name');
$names = $nameRepository->findBy([], [], $limit, $offset);
$namesCount = $nameRepository->count();

$pageCount = (int)ceil($namesCount / $limit);
$collection = new CollectionRepresentation($names);
$paginated = new PaginatedRepresentation(
   $collection,
   'user_list',
   [],
   $page,
   $limit,
   $pageCount
);

$content = $this->get('jms_serializer')->serialize($paginated, 'json');
return new JsonResponse($content,200);

如果不进行序列化,他不会向我返回任何信息,而且我也无法弄清楚问题出在哪里。通过序列化,他返回了我:

{
    "page": 1,
    "limit": 2,
    "pages": 2
}

我怀疑这是一个返回数据的问题,但我无法弄清楚问题是什么:(

0 个答案:

没有答案