How to apply new $_SERVER['REQUEST_URI'] to Slim?


#1

I have this code to make it multilingual URI.

// @link https://github.com/SimoTod/slim-multilanguage/blob/master/src/LanguageMiddleware.php Guideline.
            $uri = $request->getUri();
            $url_path = $uri->getPath();
            $url_path_explode = explode('/', $url_path);
            $all_languages = $LanguagesDb->availableLanguages();

            if (count($url_path_explode) >= 1 && isset($url_path_explode[0]) && is_array($all_languages) && array_key_exists($url_path_explode[0], $all_languages)) {
                $language_locale_uri = $url_path_explode[0];
                $url_path_explode = array_slice($url_path_explode, 1);
                if (isset($_SERVER['REQUEST_URI'])) {
                    $_SERVER['REQUEST_URI'] = $uri->getBasePath().'/'.implode('/', $url_path_explode);
                }
            }

It is in my middleware to detect language URI such as en-US/route, th/route. I can detect the language URI and remove the locale from URI and set it to $_SERVER['REQUEST_URI'].
Example: slim/public/th/contact will becomes /slim/public/contact to work with any current routers without modify them.
But I don’t know how to apply this new $_SERVER[‘REQUEST_URI’] to make Slim use this new value.


Cannot update request in Slim container
Cannot update request in Slim container
#2

OK, I got it :grin:.

$uri = $uri->withPath(implode('/', $url_path_explode));
$request = $request->withUri($uri);