Blog pagination URL with route

Hi!

I’m trying to use mysite.com/blog/page/2 instead of mysite.com/blog/page:2.

To do this, I have this route :

[
  'pattern' => 'blog/page/(:num)',
  'action' => function ($pagenum) {
    return page('blog')->render([
       'pagenum' => $pagenum
    ]);
  }
]

My pagination :


  <?php if($pagination->hasPrevPage()): ?>
  <a href="<?= page('blog')->url() . "/page/" . $pagination->page() - 1 ?>">
    ← Previous posts
    <?php var_dump($pagination->page()) ?>
  </a>
  <?php endif ?>



  <?php if($pagination->hasNextPage()): ?>
  <a href="<?= page('blog')->url() . "/page/" . $pagination->page() + 1 ?>">
    Next posts β†’
  </a>
  <?php endif ?>


And in my controller :

$articles   = $articles->paginate(10, intval($pagenum));
$pagination = $articles->pagination();

It work, but when you try to get a page that doesn’t exists, like mysite.com/blog/page/18, the Pagination page 5 does not exist, expected 1-2 error occurs instead of a 404.

How can I prevent this?
Thanks!

Hm, if there is no better solution, you could check $pagenum in your controller against number of pages divided by limit (and round to floor), if $pagenum is higher, send user to error page.