Hide Page from the Router

Hi,

I’m overwriting the isListed function in a page model to implement some custom logic to determine if a page should be visible or not:

class MyPagePage extends Page {
  public function isListed(): bool {
    $test = // some logic here ...;
    return $test;
  }
}

This works fine for $page->children()->listed() queries and is from the end user perspective what I want to achieve. However, if I enter the URL of the now unlisted page by hand, it still gets shown. I’m not sure if this is really a problem, but is there any way to also hide the page from the router via some custom logic in the model?

Thank you,
Georg

All listed and unlisted pages are accessible via their URL. Only drafts are only visible for logged in users. But you could use a route of course to catch these pages and redirect to the error page.

Okay, thank you. I was thinking about using a controller

'controllers' => [
    'default' => function($page) {
        if ($page->isUnlisted()) {
        die(go(site()->homePage()->url()));
      }
      return [];
    };
  ],

This works fine if I set the template name by hand, but I wanted to scope the controller to the entire page. It seems, the default key to have the controller for all templates is no longer working?