Browse Source

pokedex pagination and refacto

master
art.dambrine 4 years ago
parent
commit
1841028151
  1. 2
      .idea/php.xml
  2. 1
      composer.json
  3. 147
      composer.lock
  4. 1
      config/bundles.php
  5. 14
      config/packages/paginator.yaml
  6. BIN
      public/favicon.ico
  7. BIN
      public/icon.png
  8. 23
      src/Controller/PokemonController.php
  9. 6
      symfony.lock
  10. 9
      templates/base.html.twig
  11. 0
      templates/bundles/ApiPlatformBundle/SwaggerUi/index.html.twig
  12. 4
      templates/pokemon/index.html.twig

2
.idea/php.xml

@ -107,6 +107,8 @@
<path value="$PROJECT_DIR$/vendor/willdurand/negotiation" />
<path value="$PROJECT_DIR$/vendor/fig/link-util" />
<path value="$PROJECT_DIR$/vendor/api-platform/core" />
<path value="$PROJECT_DIR$/vendor/knplabs/knp-paginator-bundle" />
<path value="$PROJECT_DIR$/vendor/knplabs/knp-components" />
</include_path>
</component>
<component name="PhpProjectSharedConfiguration" php_language_level="7.2">

1
composer.json

@ -13,6 +13,7 @@
"doctrine/doctrine-bundle": "^2.3",
"doctrine/doctrine-migrations-bundle": "^3.0",
"doctrine/orm": "^2.8",
"knplabs/knp-paginator-bundle": "^5.4",
"nelmio/cors-bundle": "^2.1",
"phpdocumentor/reflection-docblock": "^5.2",
"sensio/framework-extra-bundle": "^5.1",

147
composer.lock

@ -4,7 +4,7 @@
"Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies",
"This file is @generated automatically"
],
"content-hash": "ae2e9c67c9b1291a357031a97bb24bb9",
"content-hash": "7f19c1fbf5a162c6ac24cf3f55978592",
"packages": [
{
"name": "api-platform/core",
@ -1683,6 +1683,151 @@
],
"time": "2021-01-14T21:52:44+00:00"
},
{
"name": "knplabs/knp-components",
"version": "v3.0.2",
"source": {
"type": "git",
"url": "https://github.com/KnpLabs/knp-components.git",
"reference": "7db2eb032591ded5809455af8a4dfdfda079041c"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/KnpLabs/knp-components/zipball/7db2eb032591ded5809455af8a4dfdfda079041c",
"reference": "7db2eb032591ded5809455af8a4dfdfda079041c",
"shasum": ""
},
"require": {
"php": "^7.3 || ^8.0",
"symfony/event-dispatcher-contracts": "^1.1 || ^2.0",
"symfony/http-foundation": "^4.4 || ^5.1"
},
"require-dev": {
"doctrine/mongodb-odm": "^2.0",
"doctrine/orm": "^2.7",
"doctrine/phpcr-odm": "^1.2",
"ext-pdo_sqlite": "*",
"jackalope/jackalope-doctrine-dbal": "^1.2",
"phpunit/phpunit": "^9.5",
"ruflin/elastica": "^7.0",
"symfony/http-kernel": "^4.4 || ^5.1",
"symfony/property-access": "^4.4 || ^5.1"
},
"suggest": {
"doctrine/common": "to allow usage pagination with Doctrine ArrayCollection",
"doctrine/mongodb-odm": "to allow usage pagination with Doctrine ODM MongoDB",
"doctrine/orm": "to allow usage pagination with Doctrine ORM",
"doctrine/phpcr-odm": "to allow usage pagination with Doctrine ODM PHPCR",
"propel/propel1": "to allow usage pagination with Propel ORM",
"ruflin/elastica": "to allow usage pagination with ElasticSearch Client",
"solarium/solarium": "to allow usage pagination with Solarium Client",
"symfony/property-access": "To allow sorting arrays"
},
"type": "library",
"extra": {
"branch-alias": {
"dev-master": "3.x-dev"
}
},
"autoload": {
"psr-4": {
"Knp\\Component\\": "src/Knp/Component"
}
},
"notification-url": "https://packagist.org/downloads/",
"license": [
"MIT"
],
"authors": [
{
"name": "KnpLabs Team",
"homepage": "https://knplabs.com"
},
{
"name": "Symfony Community",
"homepage": "https://github.com/KnpLabs/knp-components/contributors"
}
],
"description": "Knplabs component library",
"homepage": "http://github.com/KnpLabs/knp-components",
"keywords": [
"components",
"knp",
"knplabs",
"pager",
"paginator"
],
"time": "2021-03-16T13:52:01+00:00"
},
{
"name": "knplabs/knp-paginator-bundle",
"version": "v5.4.2",
"source": {
"type": "git",
"url": "https://github.com/KnpLabs/KnpPaginatorBundle.git",
"reference": "8a7255062dd9f6ff8a3a4712a4b34bdbbc9f31f7"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/KnpLabs/KnpPaginatorBundle/zipball/8a7255062dd9f6ff8a3a4712a4b34bdbbc9f31f7",
"reference": "8a7255062dd9f6ff8a3a4712a4b34bdbbc9f31f7",
"shasum": ""
},
"require": {
"knplabs/knp-components": "^2.4 || ^3.0",
"php": "^7.3 || ^8.0",
"symfony/config": "^4.4 || ^5.0",
"symfony/dependency-injection": "^4.4 || ^5.0",
"symfony/event-dispatcher": "^4.4 || ^5.0",
"symfony/http-foundation": "^4.4 || ^5.0",
"symfony/http-kernel": "^4.4 || ^5.0",
"symfony/routing": "^4.4 || ^5.0",
"symfony/translation": "^4.4 || ^5.0",
"twig/twig": "^2.0 || ^3.0"
},
"require-dev": {
"phpunit/phpunit": "^8.5 || ^9.4",
"symfony/expression-language": "^4.4 || ^5.0",
"symfony/templating": "^4.4 || ^5.0"
},
"type": "symfony-bundle",
"extra": {
"branch-alias": {
"dev-master": "5.x-dev"
}
},
"autoload": {
"psr-4": {
"Knp\\Bundle\\PaginatorBundle\\": "src"
}
},
"notification-url": "https://packagist.org/downloads/",
"license": [
"MIT"
],
"authors": [
{
"name": "KnpLabs Team",
"homepage": "http://knplabs.com"
},
{
"name": "Symfony Community",
"homepage": "http://github.com/KnpLabs/KnpPaginatorBundle/contributors"
}
],
"description": "Paginator bundle for Symfony to automate pagination and simplify sorting and other features",
"homepage": "http://github.com/KnpLabs/KnpPaginatorBundle",
"keywords": [
"bundle",
"knp",
"knplabs",
"pager",
"pagination",
"paginator",
"symfony"
],
"time": "2020-12-17T08:19:16+00:00"
},
{
"name": "laminas/laminas-code",
"version": "4.0.0",

1
config/bundles.php

@ -14,4 +14,5 @@ return [
Symfony\Bundle\MakerBundle\MakerBundle::class => ['dev' => true],
Nelmio\CorsBundle\NelmioCorsBundle::class => ['all' => true],
ApiPlatform\Core\Bridge\Symfony\Bundle\ApiPlatformBundle::class => ['all' => true],
Knp\Bundle\PaginatorBundle\KnpPaginatorBundle::class => ['all' => true],
];

14
config/packages/paginator.yaml

@ -0,0 +1,14 @@
# config/packages/paginator.yaml
knp_paginator:
page_range: 5 # number of links showed in the pagination menu (e.g: you have 10 pages, a page_range of 3, on the 5th page you'll see links to page 4, 5, 6)
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
filter_field_name: filterField # filter field query parameter name
filter_value_name: filterValue # filter value query paameter name
template:
pagination: '@KnpPaginator/Pagination/twitter_bootstrap_v4_pagination.html.twig' # sliding pagination controls template
sortable: '@KnpPaginator/Pagination/sortable_link.html.twig' # sort link template
filtration: '@KnpPaginator/Pagination/filtration.html.twig' # filters template

BIN
public/favicon.ico

Binary file not shown.

After

Width:  |  Height:  |  Size: 15 KiB

BIN
public/icon.png

Binary file not shown.

After

Width:  |  Height:  |  Size: 16 KiB

23
src/Controller/PokemonController.php

@ -5,11 +5,11 @@ namespace App\Controller;
use App\Entity\Pokemon;
use App\Form\PokemonType;
use App\Repository\PokemonRepository;
use phpDocumentor\Reflection\Types\Array_;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use Knp\Component\Pager\PaginatorInterface;
/**
* @Route("/pokemon")
@ -19,18 +19,27 @@ class PokemonController extends AbstractController
/**
* @Route("/", name="pokemon_index", methods={"GET"})
*/
public function index(PokemonRepository $pokemonRepository, Request $request): Response
public function index(PokemonRepository $pokemonRepository, Request $request, PaginatorInterface $paginator): Response
{
// Si l'utilisateur saisit un mot clé
if ($pokemonSearchTerm = $request->query->get('keyword')) {
return $this->render('pokemon/index.html.twig', [
'pokemon' => $pokemonRepository->findPokemonsWithSearchTerm($pokemonSearchTerm),
]);
$qr = $pokemonRepository->findPokemonsWithSearchTerm($pokemonSearchTerm);
} else {
// Sinon on affichera tout les pokemons
$qr = $pokemonRepository->findAll();
}
// Sinon on affiche tout les pokemons
// Paginate the results of the query
$pokemons = $paginator->paginate(
// TODO: poser la question à Simon (performances moindres avec passage du result ?)
$qr, /*query NOT result*/
$request->query->getInt('page', 1), /*page number*/
15 /*Items per page*/
);
return $this->render('pokemon/index.html.twig', [
'pokemon' => $pokemonRepository->findAll(),
'pokemon' => $pokemons,
]);
}

6
symfony.lock

@ -105,6 +105,12 @@
"friendsofphp/proxy-manager-lts": {
"version": "v1.0.3"
},
"knplabs/knp-components": {
"version": "v3.0.2"
},
"knplabs/knp-paginator-bundle": {
"version": "v5.4.2"
},
"laminas/laminas-code": {
"version": "4.0.0"
},

9
templates/base.html.twig

@ -10,18 +10,22 @@
<link rel="stylesheet" href="{{ asset('css/bootstrap.min.css') }}">
{% endblock %}
<script src="https://kit.fontawesome.com/410cd22f41.js" crossorigin="anonymous"></script>
</head>
<body>
<nav class="navbar navbar-expand-lg navbar-light bg-light">
<div class="container-fluid">
<a class="navbar-brand" href={{ path('pokemon_index') }}>Pokedex</a>
<a class="navbar-brand" href={{ path('pokemon_index') }}>
<img src={{ asset('icon.png') }} width="30" height="30" class="d-inline-block align-top" alt="">
Pokedex</a>
<button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#navbarNavAltMarkup" aria-controls="navbarNavAltMarkup" aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>
<div class="collapse navbar-collapse" id="navbarNavAltMarkup">
<div class="navbar-nav">
<a class="nav-link active" aria-current="page" href={{ path('pokemon_index') }}>Home</a>
<a class="nav-link active" aria-current="page" href={{ path('api_doc') }}>API docs</a>
</div>
</div>
</div>
@ -34,7 +38,6 @@
{% block javascripts %}
{#{{ encore_entry_script_tags('app') }}#}
<script src="{{ asset('js/bootstrap.min.js') }}"></script>
<script src="https://kit.fontawesome.com/410cd22f41.js" crossorigin="anonymous"></script>
{% endblock %}
</body>
</html>

0
templates/bundles/ApiPlatformBundle/SwaggerUi/index.html.twig

4
templates/pokemon/index.html.twig

@ -72,6 +72,10 @@
</tbody>
</table>
<div class="navigation d-flex justify-content-center">
{{ knp_pagination_render(pokemon) }}
</div>
<srcipt type="text/javascript" src="{{ asset('js/customButton.js') }}"></srcipt>
{% endblock %}

Loading…
Cancel
Save