Home > Net >  debug adding header in middleware laravel
debug adding header in middleware laravel

Time:09-25

i added header in middleware and i want to check in my controller that header that i set exists or not . the problem i cant watch headers in controller debugging? how can i do that? anyway use case for this is cors problem

this is my middleware:

public function handle(Request $request, Closure $next)
{
    return $next($request)
        ->header('Access-Control-Allow-Origin', '*');
}

and this is my controller:

public function action()
{
    dd(request()->headers->get("Access-Control-Allow-Origin")); //always null
}

CodePudding user response:

You need to call the set method:

public function handle(Request $request, Closure $next)
{
    $request->headers->set('Access-Control-Allow-Origin', '*');

    return $next($request);
}

Then you can retrieve the header:

public function action()
{
    dd(request()->headers->get("Access-Control-Allow-Origin")); // *
}
  • Related