diff --git a/app/AppKernel.php b/app/AppKernel.php index 3789b32..c8e1f81 100644 --- a/app/AppKernel.php +++ b/app/AppKernel.php @@ -16,6 +16,7 @@ public function registerBundles() new Doctrine\Bundle\DoctrineBundle\DoctrineBundle(), new Sensio\Bundle\FrameworkExtraBundle\SensioFrameworkExtraBundle(), new AppBundle\AppBundle(), + new Knp\Bundle\PaginatorBundle\KnpPaginatorBundle(), ]; if (in_array($this->getEnvironment(), ['dev', 'test'], true)) { diff --git a/app/Resources/views/components/list.html.twig b/app/Resources/views/components/list.html.twig index 9b7c223..8f479b3 100644 --- a/app/Resources/views/components/list.html.twig +++ b/app/Resources/views/components/list.html.twig @@ -1,14 +1,27 @@ -{% for product in products %} -
- -
- {{ product.title }} - + {% endfor %} +
+ +{# display navigation #} +

+ {{ knp_pagination_render(pagination) }} +

\ No newline at end of file diff --git a/app/config/config.yml b/app/config/config.yml index 3292445..c23ed38 100644 --- a/app/config/config.yml +++ b/app/config/config.yml @@ -66,3 +66,15 @@ swiftmailer: username: "%mailer_user%" password: "%mailer_password%" spool: { type: memory } + +# Knp paginator Configuration +knp_paginator: + page_range: 3 # default page range used in pagination control + default_options: + page_name: page # page query parameter name + sort_field_name: sort # sort field query parameter name + sort_direction_name: direction # sort direction query parameter name + distinct: true # ensure distinct results, useful when ORM queries are using GROUP BY statements + template: + pagination: KnpPaginatorBundle:Pagination:twitter_bootstrap_v3_pagination.html.twig # sliding pagination controls template + sortable: KnpPaginatorBundle:Pagination:sortable_link.html.twig # sort link template \ No newline at end of file diff --git a/composer.json b/composer.json index 88499d0..649d813 100644 --- a/composer.json +++ b/composer.json @@ -21,7 +21,8 @@ "symfony/polyfill-apcu": "^1.0", "sensio/distribution-bundle": "^5.0", "sensio/framework-extra-bundle": "^3.0.2", - "incenteev/composer-parameter-handler": "^2.0" + "incenteev/composer-parameter-handler": "^2.0", + "knplabs/knp-paginator-bundle": "^2.5" }, "require-dev": { "sensio/generator-bundle": "^3.0", diff --git a/readme.md b/readme.md new file mode 100644 index 0000000..4a58306 --- /dev/null +++ b/readme.md @@ -0,0 +1,8 @@ +# CodeCamp project + +## Used bundle + +### KnpPaginatorBundle +Pagination +https://packagist.org/packages/knplabs/knp-paginator-bundle +https://github.com/KnpLabs/KnpPaginatorBundle diff --git a/src/AppBundle/Controller/CategoryController.php b/src/AppBundle/Controller/CategoryController.php index 6e6a457..3bf9336 100644 --- a/src/AppBundle/Controller/CategoryController.php +++ b/src/AppBundle/Controller/CategoryController.php @@ -31,8 +31,26 @@ public function categoryDetail(Request $request) throw new NotFoundHttpException("Kategorie neexistuje"); } + // paginator + $em = $this->get('doctrine.orm.entity_manager'); + $dql = 'SELECT p + FROM AppBundle\Entity\Product p + JOIN AppBundle\Entity\ProductCategory pc WITH p = pc.product + JOIN AppBundle\Entity\Category c WITH pc.category = c + WHERE c.left >= :lft and c.right <= :rgt + GROUP BY p'; + $query = $em->createQuery($dql) + ->setParameter("lft", $category->getLeft()) + ->setParameter("rgt", $category->getRight()); + $paginator = $this->get('knp_paginator'); + $pagination = $paginator->paginate( + $query, /* query NOT result */ + $request->query->getInt('page', 1) /*page number*/, + 6 /*limit per page*/ + ); + return [ - "products" => $this->getDoctrine()->getRepository(Product::class)->findByCategory($category), + "pagination" => $pagination, "categories" => $this->getDoctrine()->getRepository(Category::class)->findBy( [ "parentCategory" => $category, diff --git a/src/AppBundle/Controller/HomepageController.php b/src/AppBundle/Controller/HomepageController.php index 1abe442..b67ad51 100644 --- a/src/AppBundle/Controller/HomepageController.php +++ b/src/AppBundle/Controller/HomepageController.php @@ -4,6 +4,7 @@ use AppBundle\Entity\Product; use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template; use Symfony\Bundle\FrameworkBundle\Controller\Controller; +use Symfony\Component\HttpFoundation\Request; use Symfony\Component\Routing\Annotation\Route; /** @@ -15,16 +16,21 @@ class HomepageController extends Controller * @Route("/", name="homepage") * @Template("homepage/homepage.html.twig") */ - public function homepageAction() + public function homepageAction(Request $request) { + // paginator + $em = $this->get('doctrine.orm.entity_manager'); + $dql = 'SELECT p FROM AppBundle:Product p'; + $query = $em->createQuery($dql); + $paginator = $this->get('knp_paginator'); + $pagination = $paginator->paginate( + $query, /* query NOT result */ + $request->query->getInt('page', 1) /*page number*/, + 6 /*limit per page*/ + ); + return [ - "products" => $this->getDoctrine()->getRepository(Product::class)->findBy( - [], - [ - "rank" => "desc" - ], - 21 - ), + "pagination" => $pagination, "categories" => $this->getDoctrine()->getRepository(Category::class)->findBy( [ "level" => 0, diff --git a/web/config.php b/web/config.php index 1368c8a..a031a3a 100644 --- a/web/config.php +++ b/web/config.php @@ -38,9 +38,216 @@ Symfony Configuration Checker - - -