Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Aaronharun's avatar

[5.0] What's the proper way to create a new Request in Middleware.

I have urls that are prefixed with the locale, I want to save the local and modify the request without redirecting it, what's the proper way to modify the request before passing it on?

So for a url like site.com/en/someurl

class GetLocale implements Middleware {
 public function handle($request, Closure $next)
 {
    $segment = Request::segment(1); // 'en'
    if( is_valid_language($segment) ){
         // remove local from the request url
         // do something to remove the local and continue
         // as if the url was site.com/someurl
         return $next($request);
    }

    return $next($request);
 }

}

After GetLocale runs, the rest of the site should totally ignore the locale.

(This is so we can use locales and route annotations at the same time and still keep the original headers, post data etc.)

0 likes
10 replies
usman's avatar

Hi, I have not tried it, but this should work:

    public function handle($request, Closure $next)
    {
        $dupRequest = $request->duplicate();
        $segment = $dupRequest->segment(1);
        if(is_valid_language($segment))
        {
           $request->server->set('REQUEST_URI',str_replace($segment,'',$dupRequest->path()));
           return $next($request);
        }
    return $next($request);
    }

Best Regards!

Update:// it's working.

Aaronharun's avatar

With a couple modifications, I got it working in the Middleware, but it didn't find the correct route? Did it work all the way through serving the request for you?

The REQUEST_URI does change, but it doesn't modify the $request->path. I modified it to return $dupRequest and found that it works through the other Middleware but doesn't work with routing (Laravel uses the original $request).

usman's avatar

It worked fine for me and reached the intended route properly... I even did an echo $request->path() inside the route action and it printed the correct value i.e. the changed one.

After looking at your git hub link it looks like you are setting the REQUEST_URI on the duplicated request (see above) you have to set it for the original request.

You are doing it the wrong way, It's not a bug read the above code carefully and it will work.

Aaronharun's avatar

Read my reply on github. This is Middleware that runs on /every/ request not run at the controller level. Trust me, I've been testing it. It works as I posted up until the one bug I reported.

Aaronharun's avatar

I've seen that, but I don't prefer not to prefix all my routes with a {locale} parameter. It's not convenient when using the new route annotations.

I'd prefer to keep the locale/translation as a drop-in package and removable without modifying all my controllers.

pmall's avatar

Just use a route group for the locale parameter

Aaronharun's avatar

Route groups work with route annotations (the new docbloc style routes?)

Please or to participate in this conversation.