Jump to content

How to remove page prefix in paginated URL


SoapyDaz
 Share

Recommended Posts

TL;DR How to change a paginated URL so it reads "example.com/foo/2/" instead of "example.com/foo/page2/"?

Excuse me if this is the wrong place to post this (in which case please move this to the right forum), or this has been asked before... but how do you remove the "page" prefix in a paginated url so that it reads something like "example.com/foo/1/" instead of the default of "example.com/foo/page1/"?

I know that for any other type of prefix, I can just override $config->pageNumUrlPrefix in site/config.php, but I can't figure out how you'd do it if you simply want an effectively "blank" prefix. I've tried setting $config->pageNumUrlPrefix = '' but that ends up breaking pagination altogether or the page prefix ends up in the URL instead. The only way I can think of doing this is actually changing the line:

if(!$pageNumUrlPrefix) $pageNumUrlPrefix = 'page';

to

if(!$pageNumUrlPrefix) $pageNumUrlPrefix = '';

but tampering a module file seems bad practice and so I am looking for a better way to achieve this. Help a ProcessWire noob out!

  • Like 1
Link to comment
Share on other sites

Maybe you can create your own pagination

$pageNumber = $sanitizer->selectorValue($input->urlSegment(2)); // wil get 2 from example.com/foo/2/

if(empty($pageNumber) || !is_numeric($pageNumber) || $pageNumber < 0) {
  $pageNumber = 0;
}

$limit = 10;
$base = "template=post";
$query = $base . ", start=" . (($pageNumber > 0) ? $pageNumber + $limit :  $pageNumber);
$query = $query . ", limit=$limit";

// template=post, start=0, limit=10                                       
$results = $pages->find($query);
  
$total = $pages->count($base);
  
$current = $pageNumber;
$next = $pageNumber++;

if($next > $total){
  $next = $total;
}
  
$prev = $pageNumber--;
if($prev < 0) {
	$prev = '';
}

 

  • Like 2
Link to comment
Share on other sites

Why no prefix?

I would rather use the Pager with a blank PageArray and trick it a little.

Configure your prefix to use something unique like "toreplaceprefix".

Set your template to allow page numbers AND url segments.

Then do something like this:

 

$limit = 2;
$start = $input->urlSegment1 ? ($input->urlSegment1 - 1) * $limit : 0;
$pageNum = $input->urlSegment1 ? : 1; // page number for pager

$pa = $pages->find("template=basic-page, start=$start, limit=$limit");

foreach($pa as $p){
    $content .= "<p>$p->title</p>";
}

$pagination = new PageArray();
$pagination->setTotal($pa->getTotal());
$pagination->setLimit($limit);
$pagination->setStart($pageNum);
$pagerHTML = $pagination->renderPager(array(
    "baseUrl" => $page->url,
    ));

$pagerHTML = str_replace("toreplaceprefix", "", $pagerHTML);
$content .= $pagerHTML;

 

  • Like 5
Link to comment
Share on other sites

Thank you to everyone who has responded - good to know that I haven't overlooked an option and rather it is because it is not officially supported!

@clsource: I'm guessing the first line of your code wouldn't work out of the box, if the URL to start with has the prefix "page" (or whatever I configured it to)? Or do I have to artificially create the URL such that it only says "/2/" (where 2 can be any page number)?

@Soma: Thank you for the code snippet - seems to work like a charm! The reason for no prefix is merely a cosmetic one rather than a functional one, so it's nice to know that it can be worked around even though it is not officially supported at the moment.

  • Like 1
Link to comment
Share on other sites

Tested the code snippet again more thoroughly and found that this line:

$pagination->setStart($pageNum);

causes the pagination to behave weirdly when I am on page 4+, changing it to:

$pagination->setStart($start);

seems to have fixed it. It looks like setStart takes the start offset rather than page number.

Link to comment
Share on other sites

Thanks for the report. I was having trouble with it and it wouldn't work with the offset. I was wondering. Now that you mention it, maybe it has to do cause I used limit 2? I will have a look at it again cause maybe it's a bug.

Link to comment
Share on other sites

@Soma I've tested it with bigger limits (like 5, 10 etc) and it still doesn't behave properly unless $start is passed to $pagination->setStart as I've suggested before.

Actually, just looked at the relevant documentation here and here's what it says:

Quote

PaginatedArray::setStart(int $numStart)

Set the starting offset number to use for pagination.

This is typically the current page number (minus 1) multiplied by limit setting.

which is exactly what your

$start = $input->urlSegment1 ? ($input->urlSegment1 - 1) * $limit : 0;

line does.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...