8

I'm new to laravel and am successfully directing users tothe appropriate views from a controller, but in some instances I want to set an http status code, but it is always returning a response code of 200 no matter what I send.

Here is the code for my test controller function:

public function index()
{
    $data=array();

    return response()->view('layouts.default', $data, 201);
}

If I use the same code within a route, it will return the correct http status code as I see when I call the page with curl -I from the command line.

curl -I http://localhost/

Is there a reason why it doesn't work within a controller, but does within a route call?

I'm sure there is something I'm just misunderstanding as a newbie, but even the following code works in a route but not a controller:

public function index()
{
    abort(404);
}

What am I doing wrong?

8
  • 1
    I don't want to redirect, I want to load a specific view but have it response with a specific http_response_code. Commented Feb 20, 2018 at 2:42
  • I don't want to do a redirect. I just want to set the status code. Commented Feb 20, 2018 at 2:51
  • I've updated my answer. Commented Feb 20, 2018 at 2:58
  • 1
    Copying your code, gave me 201. Clean Laravel 5.6 installation. Are you positive that your route is pointing to the right controller and method? laravel.com/docs/5.6/responses#view-responses. Commented Feb 20, 2018 at 3:01
  • Yes. I'm positive. Did you try it from within a controller? Or a route? It works fine from a route, but not the controller (though the controller will return the proper view, just not with the proper http response/status code. I'm also using a fresh install of laravel 5.6 and playing with my first controller. (OS X 10.13.1 with PHP 7.1.7) Commented Feb 20, 2018 at 3:11

1 Answer 1

25

Solution

You could use what is mentioned here. You will need to return a response like this one:

public function index()
{
    $data = ['your', 'data'];

    return response()->view('layouts.default', $data)->setStatusCode(404); 
}   //                                                ^^^^^^^^^^^^^^^^^^^

Notice the setStatusCode($integer) method.

Alternative

You could set an additional header when returning the view to specify additional data, as the documentation states:

Attaching Headers To Responses

Keep in mind that most response methods are chainable, allowing for the fluent construction of response instances. For example, you may use the header method to add a series of headers to the response before sending it back to the user:

return response($content)
            ->header('Content-Type', $type)
            ->header('X-Header-One', 'Header Value')
            ->header('X-Header-Two', 'Header Value');
Sign up to request clarification or add additional context in comments.

6 Comments

Thanks, I tried return response()->view('layouts.default', $data)->setStatusCode(404); and likewise it works when I call it from within a route in the web.php file, but if I move it so that this code is running from within a controller it works fine in every aspect except the http status code returns 200 when executed from the controller.
@climbd nope, I've just test it and it returns the proper status code (btw I used Postman to test it)
I'm going to download postman and try it, but I'm not sure why I get different results using curl -I when a test from a route vs a controller.
@climbd try to do curl -I http://the_route_of_your_request, I've done it and it shows the correct status code.
I did. Everything you suggested was right-on. So thank you for your help and sanity check. The issue was from outside of laravel. I had been playing with different ways of integrating wordpress alongside laravel by trying different methods, corcel, a native install of wordpress, etc. Well the native install of wordpress that I had left was getting in the way. Once I removed the wordpress code it worked fine. I then I was able to narrow it down to WP's template-loader.php file which I presume was loading and outputting blank data causing the HTTP headers to get send prematurely.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.