Where to place new objects created by middleware?

PSR-7 will be standardized soon (I think). This made me think of middlemen like Phly , StackPHP and ConnectJS .

ConnectJS's way is that it modifies the request object when the middleware needs to add something. For example, cookie-session creates an sessionobject property req:

app.use(session({
  keys: ['key1', 'key2']
}))

app.use(function (req, res, next) {
  var n = req.session.views || 0 // <-- req.session is managed by the session middleware
  req.session.views = ++n 
  res.end(n + ' views')
})

With PSR-7, both of our Request and Response objects are (presumably) immutable, so how do we transmit additional data like this? that is, where is the best place to store the "session" object or the "user" object created by the middleware authentication?

+4
source share
2 answers

The request and response objects in PSR-7 are implemented as value objects, so they are immutable.

Each time you need another object, you create a new instance from the previous one, for example

$newRequest = $oldRequest->withMethod('GET');

and from this point of use a new instance.

middlewares next(), (, ).

, , ServerRequestInterface withAttribute withAttributes, .

, , .

+3

. . :

function doSomething(reqest, response, session, user, foo, bar, ...)

.

-1

All Articles