panthro
panthro

Reputation: 24059

How can I set a response using a var?

In the laravel docs it has:

return response($content);

What format is $content?

I want to return it via a var like so:

$res = [null, 204];
return response($res);

But the above fails, just returns a 200.

How can I set a response using a var?

Upvotes: 1

Views: 90

Answers (2)

Alex
Alex

Reputation: 1198

response method like this. response($contents, $statusCode) It's a method of instance Response Object. as you can see that method is getting two parameters. one is contents that you are trying to return to client side. it depending what Content-Type is set in header.

so you can try like this.

$contents = "something whatever";
$statusCode = 204;

return response($contents, $statusCode);

Upvotes: 2

ceejayoz
ceejayoz

Reputation: 180075

$content can be various things - null, a string, an array.

Status codes are set via a second parameter, not an array element. Passing it an array results in a JSON response, so you're creating a 200 status JSON response of [null,204] with your code.

You fundamentally need to do:

return response(null, 204);

What you can potentially do is generate a response as a variable:

$res = response(null, 204);

and then return $res; elsewhere.

Upvotes: 2

Related Questions