Routes | Find page by translated slug

Hi all!

I dug into the form threads but did not find a suitable answer for kirby v3.

Basically i want to hide the url-slug for my page “landingpages” and access the children directly from root. in my non multi-lang setup, i use a model to return “just the uid” when using the url function. In the router, i use these patterns, which search the page based on the uri given.

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

I assume the model will work with the uri method, since it returns the translated slug. But i can’t get my router to work properly. I thought i could find my page by uri or slug, but neither does work. Does anybody has an idea how to find the page by translated slug in the router?

 [
  'pattern' => '(de/|de)?(:any)',
  'action'  => function($lang, $uid) {
     $language = trim($language, '/');
     if(!$language) $language = 'en';

    $page = page($uid);
   if(!$page) $page = pages()->findBy('slug', $uid); --> Does not work.
   if(!$page) $page = pages()->findByUri($uid); --> Does not work.


    if(!$page) $page = site()->errorPage();
    return site()->visit($page);

  }
]

Little Update

I also tried implementing the example from the docs. This does work in english and also in german for the “normal” pages, but the subpages of “landingpages” are not found either.

       [
        'pattern' => '(:any)',
        'language' => 'en',
        'action' => function($language, $slug) {
            if (page($slug)) {
                return $this->next();
            }

            if ($page = page('landingpages/' . $slug)) {
                return $page;
            }

            return false;
        }
    ],
    [
        'pattern' => '(:any)',
        'language' => 'de',
        'action' => function($language, $slug) {
            dump($slug);

            if (page($slug, $language)) {
                return $this->next();
            }

            if ($page = page('landingpages/' . $slug, $language)) {
                return $page;
            }

            return false;
        }
    ]

Im currently away from my notebook enjoying the nice weather on the seaside Will look into this when back

Hm, doesn’t work as expected for the non-default language. Don’t know if this is a bug or something wrong with the syntax.

Thanks for your help! I will try to figure out something.

After a few more tests, this is the best work around for me.

[
        'pattern' => '(:any)',
        'language' => 'en',
        'action' => function($language, $slug) {
            if ($page = page('landingpages/' . $slug)) {
                return $page;
            }

            if (page($slug)) {
                return $this->next();
            }

            return false;
        }
    ],
    [
        'pattern' => '(:any)',
        'language' => 'de',
        'action' => function($language, $slug) {   

            foreach(site()->find('landingpages')->children() as $child) {
                $childslug = $child->slug('de');
                if($slug == $childslug) {
                    return site()->visit($child, 'de');
                }
            }

            if (page($slug, $language->code())) {
                return $this->next();
            }

            return false;
        }
    ]
1 Like