I’m trying to redirect a bunch of subpages to a specific point of the parent (or other) page using URL hashtags.
E.g.:
/faa/fee/fii/foo/fuu
to
/faa/fee/fii/foo#fuu
Now the issue comes in with multilang. I’ve managed to make it work, however I feel like the code is unnecessarily long and quite clunky.
c::set('routes', array(
array(
'pattern' => '(:any)/(:all)/foo/(:any)',
'action' => function($lang, $all, $uid) {
if($lang == 'es') {
$page = page($all.'/foo');
return go(site()->url() . '/es/' . $page->uri() . '#' . $uid);
} else {
// in this case $lang is actually the first directory :(
$page = page($lang . '/' . $all . '/foo');
return go(site()->url() . '/' . $page->uri() . '#' . $uid);
}
}
)
)
Since I can’t use go()
nor page()
with a language param, and since page won’t find the page if I pass the full URL with the language code included, I try to catch the URL in bits and reconstruct both the actual URL as the translated one manually. It bothers me in particular that the first wildcard brackets in my pattern is supposed to catch the language code but it actually just catches the first directory, as my filtering is poor.
I get the feeling that using something like (es/?)
(which I’ve seen used in other threats) I could make the code much cleaner and ideally not even need the if statement.
This code only works for es/
pages, but I supect it’s just a matter of syntax for it to work also in default language. The logic to me seems correct…
c::set('routes', array(
array(
'pattern' => '(es/?)(:all)/foo/(:any)',
'action' => function($lang, $all, $uid) {
$page = page($all . '/foo');
return go(site()->url() . '/' . $lang . $page->uri() . '#' . $uid);
}
)
)