Skip to content

Instantly share code, notes, and snippets.

@memphys
Created February 24, 2015 06:29
Show Gist options
  • Save memphys/ac6777bdef81e5ca0237 to your computer and use it in GitHub Desktop.
Save memphys/ac6777bdef81e5ca0237 to your computer and use it in GitHub Desktop.

Revisions

  1. Roman Lapin created this gist Feb 24, 2015.
    49 changes: 49 additions & 0 deletions gistfile1.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,49 @@
    <?php
    namespace App\ApiBundle\Controller;

    use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;
    use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
    use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
    use FOS\RestBundle\Controller\Annotations\View;
    use Nelmio\ApiDocBundle\Annotation\ApiDoc;
    use Symfony\Component\HttpFoundation\Request;

    class ProductController extends BaseController
    {

    /**
    * @View(serializerGroups={"productList"})
    * @Route("/products")
    * @Method({"GET", "OPTIONS"})
    *
    * @ApiDoc(
    * section="Продукты",
    * description="Получение страниц по id каталога",
    * parameters={
    * {"name"="shop", "dataType"="integer", "description"="ID магазина", "required"=false, "readonly"=false}
    * },
    * output={
    * {"items", "array", "Product"},
    * }
    * )
    */
    public function listAction(Request $request)
    {
    $repository = $this->getDoctrine()->getManager()->getRepository('AppBundle:Product');

    $requestQuery = $this->get('request');
    $perPageCount = $requestQuery->get('perPage', 100);

    $query = $repository->getProductsByParams([
    'store' => $request->query->get('shop'),
    ]);

    $pagination = $this->get('knp_paginator')->paginate($query, $requestQuery->get('page', 1), $perPageCount);

    return [
    'items' => $pagination->getItems(),
    'pagination' => $pagination->getPaginationData()
    ];
    }

    }