Call the undefined method Symfony \ Component \ HttpFoundation \ Response :: header ()

Hi, I used cors middleware which seems to work fine until I added Laravel passport, now there is a problem with it .. it shows an error

 Call to undefined method Symfony\Component\HttpFoundation\Response::header() on line number 36 

This is my middleware:

<?php

namespace App\Http\Middleware;

use Closure;
use Illuminate\Support\Facades\Response;

class Cors
{

    /**
     * Handle an incoming request.
     *
     * @param \Illuminate\Http\Request $request
     * @param \Closure $next
     * @return mixed
     */
    public function handle($request, Closure $next)
    {

// ALLOW OPTIONS METHOD
        $headers = [
            'Access-Control-Allow-Origin' => '*',
            'Access-Control-Allow-Methods' => 'POST, GET, OPTIONS, PUT, DELETE',
            'Access-Control-Allow-Headers' => "Access-Control-Allow-Headers, Origin,Accept, X-Requested-With, Content-Type, Access-Control-Request-Method, Authorization , Access-Control-Request-Headers"
        ];


        if ($request->getMethod() == "OPTIONS") {
// The client-side application can set only headers allowed in Access-Control-Allow-Headers
            return Response::make('OK', 200, $headers);
        }

        $response = $next($request);
        foreach ($headers as $key => $value)
            $response->header($key, $value);
        return $response;
    }

}

the problem occurs after the if condition .. Any help would be appreciated thanks

+8
source share
3 answers

, . , Passport, . . , , , Response, middleware, Illuminate\Http\Response, Response, header('Header-Key', 'Header-Value'), , Passport, Symfony\Component\HttpFoundation\Response Call to undefined method Symfony\Component\HttpFoundation\Response::header()

, , . , , , .

$response = $next($request);
$IlluminateResponse = 'Illuminate\Http\Response';
$SymfonyResopnse = 'Symfony\Component\HttpFoundation\Response';
$headers = [
    'Access-Control-Allow-Origin' => '*',
    'Access-Control-Allow-Methods' => 'POST, GET, OPTIONS, PUT, PATCH, DELETE',
    'Access-Control-Allow-Headers' => 'Access-Control-Allow-Headers, Origin,Accept, X-Requested-With, Content-Type, Access-Control-Request-Method, Authorization , Access-Control-Request-Headers',
];

if($response instanceof $IlluminateResponse) {
    foreach ($headers as $key => $value) {
        $response->header($key, $value);
    }
    return $response;
}

if($response instanceof $SymfonyResopnse) {
    foreach ($headers as $key => $value) {
        $response->headers->set($key, $value);
    }
    return $response;
}

return $response;

Kernel.php

protected $middleware = [
    \App\Http\Middleware\Cors::class,
    // ....
];
+20

, HttpFoundation\Response, header. headers HttpFoundation\Response.

foreach ($headers as $key => $value)
    $response->headers->set($key, $value);
return $response;
+7

I got this problem when I did this

return Response::stream($callback, 200, $headers);

I solve this by simply pushing a slash (\) before Response as follows:

return \Response::stream($callback, 200, $headers);

Try this solution: Thanks

0
source

Source: https://habr.com/ru/post/1016733/


All Articles