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!

1
  • this is hardcoded in url() method on the AbstractPaginator class. look here if you are eager to do it: stackoverflow.com/a/22442404/3399676 Commented Jul 14, 2016 at 12:10

1 Answer 1

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 :)

Sign up to request clarification or add additional context in comments.

2 Comments

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.
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() .

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.