I have built a simple website which lists all pages inside a “main” page on the landing page. Using custom routes, I am able to filter these posts by a given category in the url slug:
// config.php
'routes' => function($site) {
return [
[
'pattern' => '(:any)',
'action' => function ($slug) {
if (site()->find($slug)) {
// render page normally if it exists,
// i.e "mysite.test/info"
return page($slug)->render();
} elseif (page('main/' . $slug)) {
// go to page if it is a page inside the "main" folder,
// i.e "mysite.test/main/page" (becomes "mysite.test/page")
$page = page('main/' . $slug);
return site()->visit($page);
} else {
// use slug as a filter,
// i.e "mysite.test/My+Filter"
return page('home')->render([
'tag' => $slug
]);
}
}
],
[
'pattern' => '/main/(:any)',
'action' => function($uid) {
go($uid);
}
]
];
},
unfortunately, I found out today that the site needs to use multiple languages and a frontend language switch.
I’ve added the necessary lines in the config file and updated the panel and content accordingly (english is the main language and german should be using a slug), but the routing is tough and I could use some guidance to rewrite my code properly.
I’ve tried adding the 'en'
scope to the existing code and adding another with a 'de'
scope, using visit()
too but it’s just breaking in all kinds of places… is there maybe a way I can rewrite my original routes so it would be easier to add language support?