we work on a multi language website.
i have written a little plugin that can swap the content of a page and it does this through an examination on ‘routes’. If the content has to be swapped, i return a different page via
return page(‘differentSlug’);
which works well. If nothing has to be swapped, i return the orignal page via
return page($slug);
where $slug was handed over by ‘action’. This also works well except for the starting pages.
Let’s say our URL is ‘/de/contact/’, Kirby will handover ‘contact’ as $slug. Returning page($slug); is no problem.
But if the URL is just ‘/de/’, Kirby will handover ‘de’ as $slug, which results in an error when trying to return page($slug).
Is my concept wrong on this, or is this a bug in Kirby?
A solution would be to check if the page exists, which should be done anyway:
'routes' => function ($kirby) {
return
[
[
'pattern' => '(:all)', // Die Route läuft für jede Seite
'language' => '*',
'action' => function ($language, $slug)
{
$p = page($slug);
if ( $p ) {
return $p;
};
$this->next();
}
]
];
},
By using $this->next() we make sure that Kirby goes to the next route that applies if there is no page. This might be one of your other routes, or Kirby’s internal routes.
Although I’m not sure Kirby should return the language code as slug.