dev-master
9999999-dev https://github.com/kosinix/paginatorAdds pagination functionality to a Silex app. Used in tandem with Paginator.
MIT
The Requires
- php >=5.3.3
silex pagination paginator
Wallogit.com
2017 © Pedro Peláez
Adds pagination functionality to a Silex app. Used in tandem with Paginator.
Adds pagination functionality to a Silex app. Used in tandem with Paginator., (*1)
require_once '/path/to/src/Kosinix/Pagination.php'; // Change this to the correct path
Inside your project directory, open the command line and type:, (*2)
composer require kosinix/pagination:dev-master --prefer-dist
Include the autoload.php found in vendor/:, (*3)
require_once '/path/to/vendor/autoload.php'; // Change this to the correct path
Example, inside a Controller Provider:, (*4)
$controllers->get('/products/{page}/{sort_by}/{sorting}', function (Request $request, Application $app, $page, $sort_by, $sorting) {
$sql = 'SELECT COUNT(*) AS `total` FROM product';
$count = $app['db']->fetchAssoc($sql);
$count = (int) $count['total'];
/** @var \Kosinix\Paginator $paginator */
$paginator = $app['paginator']($count, $page);
$sql = sprintf('SELECT
*
FROM
product
WHERE
1=1
ORDER BY %s %s
LIMIT %d,%d',
$sort_by, strtoupper($sorting), $paginator->getStartIndex(), $paginator->getPerPage());
$products = $app['db']->fetchAll($sql);
$pagination = new Pagination($paginator, $app['url_generator'], 'admin/products', $sort_by, $sorting);
return $app['twig']->render('admin/products/index.twig', array(
'products' => $products,
'pagination' => $pagination
));
})->value('page', 1)
->value('sort_by', 'created')
->value('sorting', 'asc')
->assert('page', '\d+') // Numbers only
->assert('sort_by','[a-zA-Z_]+') // Match a-z, A-Z, and "_"
->assert('sorting','(\basc\b)|(\bdesc\b)') // Match "asc" or "desc"
->bind('admin/products');
Add pagination.twig in your views:, (*5)
{% if pagination.isNeeded() %}
<ul class="pagination">
<li><a href="{{ pagination.firstPageUrl() }}">«« First</a></li>
{% if pagination.isPreviousPage() %}
<li><a href="{{ pagination.previousPageUrl() }}">« Prev</a></li>
{% endif %}
{% for page in range(pagination.getPaginator().shortPageStart(), pagination.getPaginator().shortPageEnd()) %}
{% if pagination.getPaginator().getCurrentPage() == page %}
<li class="active"><a href="{{ pagination.pageUrl(page) }}">{{page}}</a></li>
{% else %}
<li><a href="{{ pagination.pageUrl(page) }}">{{page}}</a></li>
{% endif %}
{% endfor %}
{% if pagination.isNextPage() %}
<li><a href="{{ pagination.nextPageUrl() }}">Next »</a></li>
{% endif %}
<li><a href="{{ pagination.lastPageUrl() }}">Last »»</a></li>
</ul>
{% endif %}
And use it in your view file:, (*6)
<h1>Products</h1>
{% if products %}
<table class="table table-bordered">
<tr>
<th><a href="{{ pagination.sortingUrl('id') }}">ID</a></th>
<th><a href="{{ pagination.sortingUrl('name') }}">Name</a></th>
<th><a href="{{ pagination.sortingUrl('quantity') }}">Quantity</a></th>
<th><a href="{{ pagination.sortingUrl('description') }}">Description</a></th>
<th><a href="{{ pagination.sortingUrl('created') }}">Date</a></th>
</tr>
{% for product in products %}
<tr>
<td>{{ product.id }}</td>
<td>{{ product.name }}</td>
<td>{{ product.quantity }}</td>
<td>{{ product.description }}</td>
<td>{{ product.created|date("F d, Y") }}</td>
</tr>
{% endfor %}
</table>
<div class="text-center">
{% include 'pagination.twig' %}
</div>
{% else %}
<p>No products found.</p>
{% endif %}
Thats it., (*7)
Adds pagination functionality to a Silex app. Used in tandem with Paginator.
MIT
silex pagination paginator