6

I just can't understand, and don't know where else to look, as the response status code of the following code is always 200, even if I set it to 400 in the main Response class.

class Api_Controller extends Base_Controller
{

    public function __construct()
    {
          parent::__construct();

          //header("HTTP/1.0 404 Not Found"); ##> This works
          //die();

          $test = array('1' => '2');
          die(Response::json($test, 400));
    }

What am I missing? I'm not using any extended class, just the default...

Update

This is the output of the Response::json... above: http://pastebin.com/RGcinSdg

As you can see, the output has the values that has been set... but still for some reason, returns 200

Update2

The output of var_dump(http_response_code()); is always 200

Update3 - Temporary fix

I have activated an extended version of Response::json and add the following line to it

http_response_code($status);

But I would still much like to know why doesn't it does it, the way it should

Alex
  • 7,538
  • 23
  • 84
  • 152
  • maybe output is started before you want to set the status code.. but this should be easy to spot if you have error reporting on.. – mishu Feb 05 '13 at 10:00
  • error reporting is on... I've updated the question with the output of `resonse::json` – Alex Feb 05 '13 at 10:03

5 Answers5

12

You can't return responses from controller constructors - it just doesn't fit with the flow of the request lifecycle in Laravel.

There's two ways to do this. You can either:

a) Setup a response filter that handles whatever functionality it is you're trying to achieve or b) Force a controller ACTION to return a response. This would be done like so:

class Api_Controller extends Base_Controller
{
    public $restful = true;

    public function get_index()
    {
        return Response::json($test, 400);
    }
}

It DOES work - you're just doing it incorrectly :)

Oddman
  • 3,715
  • 1
  • 16
  • 13
3

The same problem happens if you forget the return statement:

Response::json(array(
   'error' => true,
   'msg' => 'Bad request'
), 403);

instead of:

return Response::json(array(
   'error' => true,
   'msg' => 'Bad request'
), 403);
ecunado
  • 101
  • 5
3

Try the response()->json() syntax.

So, for example, to flag validation error from a custom FormResquest, you can do this:

/**
 * Get validation response for the request.
 *
 * @param  array $messages
 * @return \Symfony\Component\HttpFoundation\Response
 */
public function response(array $messages)
{
    return response()->json($messages, 422);
}
Jannie Theunissen
  • 28,256
  • 21
  • 100
  • 127
  • `\Response::json(...)` and `response()->json(...)` are the same thing; one uses a `Facade` while the other is a function. This does have merit for newer versions of Laravel though, as I think the `response()` function was introduced in Laravel 5.0 (or similar) and is referenced in the documentation over Facades. – Tim Lewis Apr 06 '17 at 14:21
0

Controller methods always have to return responses. But I don't think you can return a response from the constructor. You would need to use a filter.

bstrahija
  • 518
  • 3
  • 8
0

To keep Google Webmaster Tools happy I detect and use the following in the header:

META NAME="ROBOTS" CONTENT="NOINDEX, NOFOLLOW"
Pokechu22
  • 4,984
  • 9
  • 37
  • 62