Laravel 5 - Pretty paginator

So, I'm trying to get pagination in Laravel 5 with good URLs like localhost/ads/1 , where 1 stands for page.

As far as I know, such an operation will require an overload of AbstractPaginator or LengthAwarePaginator in order to target the modification of Database\Query\Builder .

Am I missing something, binding or injecting dependencies, or should it be possible to change the page we want to use?

+5
source share
3 answers

In the end, I had to code myself a Paginator. I am posting my solution here if it helps anyone.

Please note that the solution, while functional, requires some caution for practical use (validation); the class is simplified here to highlight the mechanism.

 <?php namespace App\Services; use Illuminate\Support\Collection; use Illuminate\Pagination\BootstrapThreePresenter; use Illuminate\Pagination\LengthAwarePaginator as BasePaginator; class Paginator extends BasePaginator{ /** * Create a new paginator instance. * * @param mixed $items * @param int $perPage * @param string $path Base path * @param int $page * @return void */ public function __construct($items, $perPage, $path, $page){ // Set the "real" items that will appear here $trueItems = []; // That is, add the correct items for ( $i = $perPage*($page-1) ; $i < min(count($items),$perPage*$page) ; $i++ ){ $trueItems[] = $items[$i]; } // Set path as provided $this->path = $path; // Call parent parent::__construct($trueItems,count($items),$perPage); // Override "guessing" of page $this->currentPage = $page; } /** * Get a URL for a given page number. * * @param int $page * @return string */ public function url($page){ if ($page <= 0) $page = 1; return $this->path.$page; } } 

To use a class, you can define a route

 Route::get('items/{page}',' MyController@getElements '); 

Then in the specified controller in getElements :

 $items = new Paginator(Model::all(),$numberElementsPerPage,url('items'),$page); 

Then you can dispose of your elements as usual. Note. I added a path option to integrate more complex projects with a good URL. Hope this helps!

+3
source

I wrote this for laravel 5.3 without negative results: PlumPrettyUrlPaginator.php

 <?php namespace Plum\Cmc\Paginator; use Illuminate\Pagination\LengthAwarePaginator; use Illuminate\Support\Collection; class PlumPrettyUrlPaginator extends LengthAwarePaginator{ /** * basically a copy of LengthAwarePaginator constructor and then replacing all in our own */ public function __construct(LengthAwarePaginator $p, $path, $currentPage = null) { $this->total = $p->total; $this->perPage = $p->perPage; $this->lastPage = (int) ceil($p->total / $p->perPage); // $this->path = ((stripos(strrev($p->path), '/') === 0) ? $p->path : $p->path.'/'); $this->path = $path; $this->currentPage = $p->setCurrentPage($currentPage ?? $p->currentPage, $p->pageName); $this->items = $p->items; } public function url($page){ if ($page <= 0) $page = 1; return $this->path.$page; } } 

routes:

 Route::get('/', [ function (Request $request) { $data = PlumSearchService::PrepareSearchView($request); return view('inventory.search_products', $data); }]); Route::get('/page/{pageNr}', [ function (Request $request, int $pageNr) { $data = PlumSearchService::PrepareSearchView($request, false, $pageNr); return view('inventory.search_products', $data); }]); 

then in PlumSearchService

 $query = Product::select('product.*') //... $paginator = $query->paginate(15, ['*'], 'page', $pageNr); $products = new PlumPrettyUrlPaginator($paginator, '/page/', $pageNr); 
+1
source

Delete this line of code RewriteRule ^(.*)/$ /$1 [L,R=301] with .htaccess .

-3
source

Source: https://habr.com/ru/post/1210039/


All Articles