Why does $response- > headers- > set in Laravel have no effect?

ready to develop Mini Program, first test the background API, project to develop using Laravl+jwt+dingo. JWT wants to find such a middleware on the Internet during painless refresh token, testing

    public function handle($request, Closure $next)
    {
        //  token 
        $this->checkForToken($request);

       //  try  token  TokenExpiredException  
        try {
            // 
            if ($this->auth->parseToken()->authenticate()) {
                return $next($request);
            }
            throw new UnauthorizedHttpException("jwt-auth", "");
        } catch (TokenExpiredException $exception) {
          //  token  TokenExpiredException  token 
            try {
                //  token
                $token = $this->auth->refresh();
               // 
                Auth::guard("api")->onceUsingId($this->auth->manager()->getPayloadFactory()->buildClaimsCollection()->toPlainArray()["sub"]);
            } catch (JWTException $exception) {
               //  refresh 
                throw new UnauthorizedHttpException("jwt-auth", $exception->getMessage());
            }
        }
        
        //  token
        return $this->setAuthenticationHeader($next($request), $token);
    }

after testing the interface in postman, there is no way to add authorization
$this- > setAuthenticationHeader in response to header after each request. The source code is:

    protected function setAuthenticationHeader($response, $token = null)
    {
        $token = $token ?: $this->auth->refresh();
        $response->headers->set("Authorization", "Bearer ".$token);

        return $response;
    }

$response- > headers- > set ("Authorization"," Bearer". $token); cannot be added here. Why?
finally switch to this way:

        return $next($request)->withHeaders([
                "Authorization"=> "Bearer ".$token,
            ]);

there should be no problem with the method that comes with dingo. Is there something wrong with me?

Oct.19,2021

. You can check the code again. First of all, there is no problem with setting up headers in this way. Look at your code is too complex, too many situations. Guess whether it is because throw exception causes laravel to re-make and response causes headers coverage?


laravel does not set header in this way.
if you want to use header ($key,$val), you can make multiple calls in succession or use withHeaders ()

Menu