Love Fuel?    Donate

FuelPHP Forums

Ask your question about FuelPHP in the appropriate forum, or help others by answering their questions.
Response don't send 404 header to the browser when throwing HttpNotFoundException
  • Hello, I'm coding with the current 1.1-dev and having some problems with HttpNotFoundException. The welcome example works as expected (shows the error and send a 404 header to the browser) but if I go a little more complex the error is shown but the header is lost (browsers receives a 200). Actually my _404_ route points to default/index, that does the next:
                    $this->response->status = 404;
                    $this->data['body']['content'] = View::forge('404', array(
                            'global' => $this->data['global'],
                            'body' => $this->data['body']));
    

    As you can see this is not sent to the browser. This Controller_Default extends Template_General that does the next:
        public function after($response)
        {
    
            if (empty($this->data['response']))
            {
                $this->data['body'] = View::forge('template/general', array(
                        'global' => $this->data['global'],
                        'body' => $this->data['body']));
            }
    
            parent::after($response);
            return true;
        }
    

    Then, this Template_General extends Template_Html. This Template_Html is where finally the view is rendered and sent to the browser:
        public function after($response)
        {
            $this->viewResponse();
    
            return Response::forge($this->response->body, $this->response->status)->send(true);
        }
    

    The viewResponse() function saves the rendered view to $this->response->body correctly. It works, it's viewed and rendered correctly and as expected. The problem is that the $this->response->status is ignored. If in all after($response) I do a dump of status everywhere I get a '404' (correct) but a 200 is sent to the browser. The most estrange behavior is that if I change the return line for this:
    Response::forge($this->response->body, $this->response->status)->send(true);die;
    

    The 404 is sent, the output is sent to the browser... all correct but I know that this is not the elegant way of doing things. I do not understand why is this happening and having dumped all the possible vars I don't see where this problem can came from. PS: The Template_Html just extends the \Controller. Thank you in advance
  • Not setting a 404 header is intended behaviour, and documented as such: http://fuelphp.com/dev-docs/general/404.html
    In short, this allows you to reply with something that isn't a non-found situation. You need this for example when you have a single front controller that gets it's routing, templates, widgets and other page data from the database. This front controller is defined as the 404 route, and will therefore always be called in a not-found situation. It would not be handy to have a 404 header set in that case. For 1.1, you're using a deprecated syntax. Controllers actions should return a response object, which is then processed by the router. For backward compatibility, if the router detects you're not returning anything from your method, it will use the response body as it did in 1.0.1. But it doesn't use anything else from that response object. This deprecated feature is going to be removed in develop as soon as 1.1 is released, so it is wise not to use it anymore.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

In this Discussion