Returning the start page via page() with slug given through routes

Hey all,

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?

Thanks in advance for your help!

Could you please post the route?

This is the easiest way to let it fail if i do it for the homepage. All other pages work.

'routes' => function ($kirby) {

        return
        [
            [
                'pattern' => '(:all)',  // Die Route läuft für jede Seite
                'language' => '*',
                'action' => function ($language, $slug)
                {

                    return page($slug);

                }
            ]
        ];
    }

Maybe i think in a wrong direction here. The goal is (if no other conditions apply) to just return the page Kirby would originally deliver.

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.

Thank you, that helped a lot! Didn’t know about $this->next();