我开始使用Symfony2框架创建一个RESTFul应用程序接口。它将只返回JSON格式的数据。我正在尝试在操作控制器中创建一个返回用户列表的方法。问题是我的代码太多了,而且不优雅。我认为我做错了什么,因为简单的SQL查询将比我的代码更简短、更清晰。
我的方法应该从URL获取4个输入参数: limit、offset、sort字段和sort direction。输出应该是包含用户数组的JSON。我想使用Symfony2序列化程序配置JSON中的字段。我想在"X- total -Count“头中返回总用户数。
据我所知,最好使用FOSRestBundle。所以,我试了一下:
<?php
namespace AppBundle\Controller;
use FOS\RestBundle\Controller\FOSRestController;
use Symfony\Component\HttpFoundation\Request;
use FOS\RestBundle\Controller\Annotations;
use FOS\RestBundle\Request\ParamFetcherInterface;
class UserController extends FOSRestController
{
/**
* List all notes.
*
* @Annotations\QueryParam(name="_page", requirements="\d+", default=1, nullable=true, description="Page number.")
* @Annotations\QueryParam(name="_perPage", requirements="\d+", default=30, nullable=true, description="Limit.")
* @Annotations\QueryParam(name="_sortField", nullable=true, description="Sort field.")
* @Annotations\QueryParam(name="_sortDir", nullable=true, description="Sort direction.")
*
* @Annotations\View()
*
* @param Request $request the request object
* @param ParamFetcherInterface $paramFetcher param fetcher service
*
* @return array
*/
public function getUsersAction(Request $request, ParamFetcherInterface $paramFetcher)
{
$sortField = $paramFetcher->get('_sortField');
$sortDir = $paramFetcher->get('_sortDir');
$page = $paramFetcher->get('_page');
$limit = $paramFetcher->get('_perPage');
$orderBy = null;
if ($sortField && $sortDir) {
$orderBy = [
$sortField => $sortDir
];
}
$offset = ($page - 1) * $limit;
$repository = $this->getDoctrine()
->getRepository('AppBundle:User');
$users = $repository->findBy([], $orderBy, $limit, $offset);
return $users;
}
...
}我认为这段代码很好,因为它很短,而且我可以使用序列化程序。并且我可以配置哪些字段应该作为响应。但在本例中,我没有response对象,也不能设置"X-Total-Count“头。此外,我不知道如何获得总行数量。
我尝试了另一个案例:
<?php
namespace AppBundle\Controller;
use Symfony\Component\HttpFoundation\JsonResponse;
use FOS\RestBundle\Controller\FOSRestController;
use Symfony\Component\HttpFoundation\Request;
use FOS\RestBundle\Controller\Annotations;
use FOS\RestBundle\Request\ParamFetcherInterface;
use Doctrine\ORM\Tools\Pagination\Paginator;
class UserController extends FOSRestController
{
/**
* List all users.
*
* @Annotations\QueryParam(name="_page", requirements="\d+", default=1, nullable=true, description="Page number.")
* @Annotations\QueryParam(name="_perPage", requirements="\d+", default=30, nullable=true, description="Limit.")
* @Annotations\QueryParam(name="_sortField", nullable=true, description="Sort field.")
* @Annotations\QueryParam(name="_sortDir", nullable=true, description="Sort direction.")
*
* @param Request $request the request object
* @param ParamFetcherInterface $paramFetcher param fetcher service
*
* @return array
*/
public function getUsersAction(Request $request, ParamFetcherInterface $paramFetcher)
{
$sortField = $paramFetcher->get('_sortField');
$sortDir = $paramFetcher->get('_sortDir');
$page = $paramFetcher->get('_page');
$limit = $paramFetcher->get('_perPage');
$offset = ($page - 1) * $limit;
$em = $this->getDoctrine()->getEntityManager();
$qb = $em->createQueryBuilder();
$qb->select('u')
->from('AppBundle:User', 'u');
if ($sortField && $sortDir) {
$qb->orderBy('u.' . $sortField, $sortDir);
}
$query = $qb->getQuery();
$query->setFirstResult($offset)
->setMaxResults($limit);
$paginator = new Paginator($query);
$totalCount = $paginator->count();
$users = $query->getResult();
$response = new JsonResponse($users);
$response->headers->set('X-Total-Count', $totalCount);
return $response;
}
...
}在这种情况下,我有一个response对象,我可以设置一个"X-Total-Count“头。我可以使用分页器获得总行数量。但是我不能像前面的例子那样使用序列化程序。我认为这段代码太多了,也不优雅。我使用查询构建器、查询、分页器来获取用户列表。
请告诉我如何使用简单,优雅的代码获取用户列表。
发布于 2015-12-15 05:07:11
您可以这样做,而不是像第一个代码示例那样只返回$users:
$view = $this
->view($users, 200);
->setHeader('X-Total-Count', $totalCount);
return $this->handleView($view);您还可以创建扩展基本存储库的存储库,并将您的分页逻辑移动到那里。这样一来,你的控制器就会变得非常薄。
https://stackoverflow.com/questions/34274034
复制相似问题