Welcome Guest, Not a member yet? Register   Sign In
Allow for route matching without need for {locale} placeholder for localized URI's
#1

CI4 requires you to add the '{locale}' placeholder within user-defined routes in order to match URI paths that *might include a locale code in the 1st segment. There's currently no way to force the router to match routes on such URI's without explicitly including the '{locale}' placeholder.

In CI3, I had to create my own custom routing logic that would automatically detect the 'locale code' from the first URI segment (if present). It would then set the locale globally for the request based whether or not it detected a locale segment. If no locale was detected, it would use the default fall-back locale (all of this is obviously similar to what CI4 does now). 

However, in contrast to how CI4 works, my solution would then completely remove the 'locale' segment from the internal URI mapping for all of CI's subsequent routing & URI logic. What this meant is that I no longer needed to constantly worry about dealing with the 'locale' segment when defining routes & dealing with URI paths / segments. 

(FYI, If I really needed to know if the original URI included the locale code, I would set a global flag during locale detection, 'uriLang = true', and use that as necessary)

Here's an example of what I'm describing in CI3 w/ my custom Router/URI handling:
Code:
// a route such as this: 
$route['product/:num'] = 'catalog/product_lookup';

// would work for all of the following URI's
// /product/100         // locale segment !found  ==> set locale to 'en' (default/fallback)
// /es/product/100    // locale segment  found  ==> set locale to 'es'
// /fr/product/100     // locale segement found  ==> set locale to 'fr'

// also, with a URI request for '/fr/product/100',
// the following code...
echo(uri_string()); 

// ...would output '/product/100', *without* the '/fr/' locale segment 

In CI4, there's still a need to add the '{locale}' placeholder within each&every defined route. Otherwise, the routes won't match. This is extra work that's unecessary and prone to error.

Also, the URI functions will redundantly include the locale within the URI, even when it's not always desired (example: when creating a 'switch language' widget that should switch the locale segment while still linking to the current URI path)

Feature Request(s):
It would be easier if there was a way to simply force CI to assume that there *can* be a locale segment present within the request URI, and, if present, to deal with it accordingly. It would also be helpful if other functions that dealt with the request URI, such as the URL helper, also provided a way to get the URI path *without* including the locale segment (if present).

I'm proposing that perhaps this functionality could be optionally enabled in CI4 via a config parameter, something like 'useLocalizedRoutes = true;' within Routing.php', or by some other means. The idea is to get CI4 to match defined routes regardlless of whether or not the locale segment is present. The logic for the HTTP Request & Router  logic would need to be adjusted accordingly.

Simplified example of how it could work:
Code:
// in CI4, instead of constantly
// having to do something like this:
$routes->get('({locale}/)?users', 'Users::list');

// ...a route such as this:
$routes->get('users', 'Users::list');

// ... could be forced to match these URIs:
// '/users'
// '/en/users'
// '/fr/users'
Reply


Messages In This Thread
Allow for route matching without need for {locale} placeholder for localized URI's - by csrf - 11-25-2023, 06:57 PM



Theme © iAndrew 2016 - Forum software by © MyBB