Syed Abdur Rehman Kazmi
Syed Abdur Rehman Kazmi

Reputation: 1647

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

Hi i was using a cors middleware which seems to work fine until i added Laravel Passport now there is a problem with it.. it shows the 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 issue is after the if condition .. Any help will be appreaciated thanks

Upvotes: 22

Views: 33347

Answers (5)

Mehmet Govori
Mehmet Govori

Reputation: 1

I solve it by placing the project in desktop . So if your project is inside a folder in desktop or some other place just place remove it from there and place it in desktop.

Upvotes: 0

Bernard Wiesner
Bernard Wiesner

Reputation: 1425

There are 2 response objects:

  • Laravel: Illuminate\Http\Response
  • Symfony: Symfony\Component\HttpFoundation\Response

Passport returns Symfony's response object.

Laravel's response object is extending Symfony's and adds some convenient methods such as header(), however that method is not defined on Symfony's response object.

You can simply always use Symfony's underlying method (that laravel extends):

$response->headers->set($key, $value);

Upvotes: 0

Pakpoom Tiwakornkit
Pakpoom Tiwakornkit

Reputation: 2939

Hi I faced the same problem. It seems like it was an error in Passport and there are many developers are in the same situation. I just found the solution to this issue. The reason why we get this error is because Response object we get in middleware is usually an instance of Illuminate\Http\Response class which we can set Response headers using the method header('Header-Key', 'Header-Value') whereas the Request handled by Passport will be an instance of Symfony\Component\HttpFoundation\Response that's why we got the error Call to undefined method Symfony\Component\HttpFoundation\Response::header()

Below is the code that I use to tackle this error and now everything works fine. I hope it helps other developers get the idea how to fix it and then adapt to their code.

$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;

And in my Kernel.php

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

Upvotes: 55

Muhammad Wajahat Anwar
Muhammad Wajahat Anwar

Reputation: 1065

I got this problem when I did this

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

I solve it by simply putting forward slash ( \ ) before Response like this:

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

Try this solution: Thanks

Upvotes: 1

Pankit Gami
Pankit Gami

Reputation: 2553

It seems that from your application you are getting the HttpFoundation\Response, which doesn't have the header method. So instead you can try to set the header to the headers variable of the HttpFoundation\Response.

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

Upvotes: 9

Related Questions