Custom Middleware setting withLocation on Response Object

I built this Middleware class to redirect the requested page if a language it is not defined on the url

For instance:
If someone goes to /users/login the application would redirect the browser to /en_US/users/login
The class in questions is:

class LanguageParameterMiddleware
    private $myDefaultLanguage = 'en_US';

    public function __invoke($request, Response $response, $next) {
           $response = $next($request, $response);
           if (!$request->getParam('lang')) {
                $response->withLocation('/' . $this->myDefaultLanguage . 
           return $response;

The response->withLocation is being executed, but no redirection is happenning. I do not see the Location header being sent back on the Firefox Developer Tools

Any idea what I am doing wrong?


Assign the new response to the $reponse variable:

 $response = $response->withLocation('/' . $this->myDefaultLanguage . 

Yes, for compatibility with PSR-7, various objects in the system are becoming immutable, so calling functions on them doesn’t change their state, but rather they return a new object that reflects the requested changes. Request and response are two of the most visible such objects.