Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pagination with URL path or the query parameters

I am working on writing a blog and came across two different alternatives on the internet for making pagination. I couldn't decide which to use. Url types are like ;

blog/page/2

blog/?page=2

Does one of these have an advantage over the other?

like image 362
safaer Avatar asked Apr 28 '18 01:04

safaer


People also ask

What is URL path parameter?

Path parameters are variable parts of a URL path. They are typically used to point to a specific resource within a collection, such as a user identified by ID. A URL can have several path parameters, each denoted with curly braces { } .

Can I pass URL as query parameter?

Yes, that's what you should be doing. encodeURIComponent is the correct way to encode a text value for putting in part of a query string. but when it is decoded at the server, the parameters of url are interpreted as seperate parameters and not as part of the single url parameter.

What are query and path parameters?

The path parameter defines the resource location, while the query parameter defines sort, pagination, or filter operations. The user's input (the query) is passed as a variable in the query parameter, while each path parameter must be substituted with an actual value when the client makes an API call.


1 Answers

Best practices are that path parameters are used to identify a specific resource, and query parameters filter or sort that resource.

If you are adding a pagination with articles, it would be ideal to use query parameters to sort through the articles. It is common for this query parameter to be referred to as offset, as you would be filtering through your articles.

So for example if you had 100 articles you've posted, and you want to display 10 articles per pagination page, and you were on page 2 of 10 in your pagination, your offset query parameter would be ?offset=10 because you would be filtering for articles 10-19 to display. (because articles 0-9 were displayed on your first pagination page)

This offset query parameter would increase by 10 every pagination page you increase, then filtering to the next 10 articles.

like image 112
schmitty890 Avatar answered Oct 05 '22 23:10

schmitty890