Jump to content

404 when using pagination on multi language (via url-presegment) site


neildaemond
 Share

Recommended Posts

i've used "Example 3: Display a different language based on URL pre-segment" from the guide to make a site multi-language capable..

my language gateway looks like:

<?php 

    $user->language = $languages->get($page->name);
    $path = '/';
    foreach($input->urlSegments as $segment) {
    $path .= $segment . '/';
    }
    $mypage = $pages->get($path);
    if(!$mypage->id || !$mypage->viewable()) throw new Wire404Exception();

//replace local navigation links
    $out = $mypage->render();
    $regex = '{(<a [^>]*href=["\']' . $config->urls->root .')(["\']';
    $topnav = $page->parent->children();
    foreach($topnav as $p) $regex .= "|" . $p->name;
    $regex .= ")}";
    $out = preg_replace($regex, '$1' . $page->name . '/$2', $out);

    echo $out; 

the regex works quite well for everything else, but but kind of skews up on the 'blog/news' page which uses pagination.

besides the fact that the pagination links end up as 'site.com/blog/blog/page2' instead of 'site.com/blog/page2', even when manually entering 'site.com/blog/page2', 4040 error is thrown.

I'm guessing that things go bad here:

    $mypage = $pages->get($path);
    if(!$mypage->id || !$mypage->viewable()) throw new Wire404Exception();

since  "path/to/page/page2" will not not return a page. Also, removing the "throw new 404" line, I end up with a blank page.

Any suggestions?

Thanks,

Link to comment
Share on other sites

Just an idea - you could try to remove the pagination from the path before getting the page:

$path = preg_replace('/page[0-9]+$/', '', $path);

Or skip the last segment:

$n = count($input->urlSegments);
$i = 1;
foreach($input->urlSegments => $segment) {
  if ($i == $n) {
    if (preg_match('/page[0-9]+/', $segment)) break;
  }  
  $path .= $segment . '/';
}
Link to comment
Share on other sites

How is the pagination segment "page2" ending up in the path? The page number isn't considered one of the URL segments returned by $input->urlSegments, so it shouldn't be showing up in there unless the "allow page numbers" option is turned off for that template. 

  • Like 1
Link to comment
Share on other sites

good question, Ryan and it lead me to the solution.

Although I had "allow page numbers" enabled on the template containing the paginationMarkup (the blog/news feed), I didn't have it enabled on the 'language-gateway' template.

So, settings "allow page numbers" on the 'language-gateway' template solved the issue :)

Perhaps we can mention this little caveat in the documentation ;)

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