Have a problem with routing

Hello, I got a problem with routing. I did everything according to the documentation, but the draf pages give a 404 error and I can’t fix it.
I tried in my project and almost in pure starter kit. The effect is the same.
https://getkirby.com/docs/guide/routing

<?php

return [
    'routes' => [
        [
            'pattern' => '(:any)',
            'action'  => function($uid) {
                $page = page($uid);
                if(!$page) $page = page('blog/' . $uid);
                if(!$page) $page = site()->errorPage();
                return site()->visit($page);
            }
        ],
        [
            'pattern' => 'blog/(:any)',
            'action'  => function($uid) {
                go($uid);
            }
        ]
    ]
];

The page helper doesn’t find drafts, so you would have to use

   [
            'pattern' => '(:any)',
            'action'  => function($uid) {
                $page = kirby()->page($uid);
                if(!$page) $page = kirby()->page('blog/' . $uid);
                if(!$page) $page = site()->errorPage();
                return site()->visit($page);
            }
        ],
1 Like

Thanks a lot! Now it works!

Hi! There was another problem, now the draft pages are public and visible for indexing by search engines. Maybe there is a way to solve this?

Block by checking for a logged-in user depending on page status

I try like this but it still opens the page


  'routes' => [
    [
      'pattern' => '(:any)',
      'action'  => function($uid) {
          $page = kirby()->page($uid);
          if($page && $page->status() == 'draft') {
            if(!site()->user() || !site()->user()->isAdmin()) {
              if(!$page) $page = site()->errorPage();
            }
          } else {
              if(!$page) $page = kirby()->page('home/' . $uid);
          }
          return site()->visit($page);
      }
    ],
    [
        'pattern' => 'home/(:any)',
        'action'  => function($uid) {
            go($uid);
        }
    ]
  ]

site()->user() does not exist.

'action'  => function($uid) {
                $page = kirby()->page($uid);
                if(!$page) $page = kirby()->page('blog/' . $uid);
                if ($page && $page->status() === 'draft' && ! kirby()->user() {
                   return site()->visit(page('error');
                }
                if(!$page) $page = site()->errorPage();
               
                return site()->visit($page);
            }
1 Like

Thank you a lot!