1

I have a laravel 5 application in which I`m trying to do a pagination for certain results (array of results not collection). This is my code:

$paginator = new LengthAwarePaginator(
 $l_aResponse['body'],
 count($l_aResponse['body']),
 '2',
 Paginator::resolveCurrentPage(),
 ['path' => Paginator::resolveCurrentPath()]
);
return str_replace('/?', '?', $paginator->render());

My question is: is there a way to modify the way in which the "page" parameter is setup in the URL for pages? EG: I don't want this format: http://localsite/articles?page=3 but I want http://localsite/articles/3

I`ll appreciate any answer. Thanks!

shAkur
  • 944
  • 2
  • 21
  • 45
  • this is hardcoded in url() method on the AbstractPaginator class. look here if you are eager to do it: http://stackoverflow.com/a/22442404/3399676 – ClearBoth Jul 14 '16 at 12:10

1 Answers1

3

The __construct() function of the LengthAwarePaginator class has a few parameters:

  • $items
  • $total
  • $perPage
  • $currentPage = null
  • array $options = []

As you can see the fourth parameter represents the current page.

So you can use a custom desired current page which has been retrieved from the last parameter from the url. An example would be:

//  http://localsite/articles/3

$currentPage = ... // Get parameter from url

$paginator = new LengthAwarePaginator(
    $l_aResponse['body'],
    count($l_aResponse['body']),
    '2',
    $currentPage, // Current page as fourth parameter
    ['path' => Paginator::resolveCurrentPath()]
);

return str_replace('/?', '?', $paginator->render());

Have not tested this. Hope it helps :)

Thomas Van der Veen
  • 3,136
  • 2
  • 21
  • 36
  • Thank you for your solution! However, this won't allow we to modify the pagination links and the links will still have ?page=1 and so on in the URL. I wanted to strip that out through some sort of parameter or so, and I didn't want to interfere and modify those links via JS or other method. – shAkur Jul 19 '16 at 06:54
  • I already thought that would happen. Maybe you should play with `'path' => Paginator::resolveCurrentPath()`. Or you could create a new paginator class that extends the current `LenfthAwarePaginator` class and overwrite some methods like `render()` . – Thomas Van der Veen Jul 19 '16 at 08:01