Extend DI Container in Middleware

Hi,
is it possible to extend the container inside a middleware?

I want to get an route parameter (exmp. id) inside a middleware.
The problem is, that I cant acces the Requests Route object inside my Container creation.
Inside a middleware I can access ist but cant extend the container.

Snipper for Container

  //... getContainer and other deps.
 $container["id"] = function($container){
   
 //Cant acces route params inside here

$data = $container["idManager"]->getSessionData();
if( !empty($data["id"]) ){
return (int) $data["id"];
}
return null;
};

Middleware

public function __invoke(Request $request, Response $response, $next)        
    $id= $request->getAttribute("route")->getArgument("id");
        
    if( !$this->_container["id"] ){
         //This doesnt works.
        $this->_container->extend("id", function($container) use($id){
            return $id;
        });
    }
}

So does anyone knows a solution.
I need the id before the route callback is dispatched.

    if( !$this->_container["customerId"] ){
         //This doesnt works.
        $this->_container->extend("customerId", function($container) use($id){
            return $id;
        });
    }

You cannot extend something that is not set yet! Try setting it normally.

if( !$this->_container["customerId"] ) {
   $this->_container["customerId"] = function($container) use($id){
        return $id;
   };

    // or
    $this->_container["customerId"] = $id;
}

Then it says:
Cannot override frozen service “id”

The error indicates that you have defined and used a service named id before. (Pretty sure it was called customerId in your example)