SVadzim
February 10, 2023, 6:52pm
1
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);
}
]
]
];
texnixe
February 10, 2023, 7:14pm
2
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
SVadzim
February 10, 2023, 7:25pm
3
Thanks a lot! Now it works!
SVadzim
February 13, 2023, 3:50pm
4
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?
texnixe
February 13, 2023, 4:51pm
5
Block by checking for a logged-in user depending on page status
SVadzim
February 13, 2023, 6:55pm
6
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);
}
]
]
texnixe
February 13, 2023, 7:20pm
7
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