使用 withAttribute 错误的 PHP Slim Framework 请求

Ani*_*ran 0 php slim

我只是尝试从 middileware auth 函数传递用户名

$request->withAttribute('username','XXXXXX');
return $next($request, $response);
Run Code Online (Sandbox Code Playgroud)

但我无法使用此用户名访问

$request->getAttribute('username');
Run Code Online (Sandbox Code Playgroud)

我找到了一个解决方案,它只有在我这样添加时才有效

 return $next($request->withAttribute('username','XXXXXX'), $response);
Run Code Online (Sandbox Code Playgroud)

是什么原因?请帮我。我需要传递多个参数 pass。我该怎么办?

Mik*_*ola 7

Request 和 Response 对象是不可变的。这意味着withAttribute()将返回$request对象的新副本。您需要返回新对象而不是原始对象。

$request = $request->withAttribute('username','XXXXXX');
return $next($request, $response);
Run Code Online (Sandbox Code Playgroud)