Go to current language from route on multi-language site

I have protected a page and want not logged in user to go to another page. It works, but it does not respect the current language of the user. It’s send to the default language version.

'routes' => [
        [
            'pattern' => ['es/productos', 'en/products'],
            'action' => function () {
                if (!kirby()->user() || kirby()->user()->role()->id() === 'solicitante') {
                    go(kirby()->languageCode(). '/login');
                }
            }
        ],

These pages do exist, don’t they? So why do you need a route? I’d put this code into the controller of the protected page, where it makes much more sense. Your routes won’t work anymore if the page slugs are changed, so you would have to prevent that. You don’t run into this problem if you put the code where it belongs.

The working solution with the route (not recommended for this use case):

 [
        'pattern' => ['productos', 'products'],
        'language' => '*', 
        'action' => function ($lang) {
            if (!kirby()->user() || kirby()->user()->role()->id() === 'solicitante') {
                return go($lang->code() . '/login');
            }
        }
]
1 Like

It works! but I’ll look intro put this on the controller has you say, thanks!