Custom routes that work in normal and multi-language setups

Dear forum,

is there a way to set up custom routes which will work in normal as well as multi-language setups?

Since this:

return [
    'routes' => [
        [
            'pattern' => '(:all)',
            'language' => '*',
            'action' => function ($language, $slug) {
                // …
            }
        ],
    ]
];

will cause an error in normal setups because the $language variable isn’t passed and this:

return [
    'routes' => [
        [
            'pattern' => '(:all)',
            'action' => function ($slug) {
                // …
            }
        ],
    ]
];

will only catch URLs without language code in multi-language setups.

I’m creating a plugin, so my routes should be as universal as possible. Thanks for any hints!

Maybe I could do something like this:

return [
    'routes' => [
        [
            'pattern' => '(:all)',
            'language' => '*',
            'action' => function ($variable1, $variable2 = null) {
                if ($variable2 !== null) {
                    $language = $variable1;
                    $slug = $variable2;
                else {
                    $language = $variable2;
                    $slug = $variable1;
                }
            }
        ],
    ]
];

I’d probably set up different routes for multi-language and standard context which then share their logic.

But whichever route definition comes first will catch the request, no? In my noted examples, any of the two routes would handle the request if first in order.

Anyways, I think my proposed solution will do for me … it may not be pretty, but it works. :slight_smile:

I rather meant to check if you have a multilang site or not