Love Fuel?    Donate

FuelPHP Forums

Ask your question about FuelPHP in the appropriate forum, or help others by answering their questions.
Response headers
  • Problem is that in fuelphp 1.1 response headers are lost. This happens because (IMO):
    1. Incorrect documentation, which says that we should use $this->response->set_headers() method, but actually Request returns:
    a) Response that returned Controller->action() or Controller->after() methods
    b) Newly created response in case Controller->action or Controller->after() returned string (in most cases \View instance) 2. Incorrect (not obvious at least) response->execute() method behavior in 1.b situation. Request->execute() should add the third argument "$this->controller_instance->response->headers" to Response::forge method. IMO, Response sent to the controller is useless. P.S.
    Why $controller->response object usage is deprecated (as stated in request class)?
    How can I set response->headers in controller->action() method, and then extend \View instance in controller->after() method ??
  • The deprecated code is still there, because of FuelPHP design rules. We never remove code from one version to the next, we deprecate first. For the next version, this will probably be removed, since $this->response is no longer used, it has become pointless to pass it to the controller constructor. Your controller actions should return a Response object. You can set headers on it when you return it.
    return \Response::forge('whatever-you-want-to-return')->set_header($name, $value);
    

    or if you want to do it in multiple steps
    $response = \Response::forge();
    $response->body('whatever-you-want-to-return');
    $response->set_header($name, $value);
    return $response;
    
  • I understand all said above. I just pointing that it in current situation (docs + fuelphp1.1) not obvious that in case we return string from action, response is lost.
    And by changing 1 line we improve compatibility, till Response is removed from controller.
    My 0.02$
  • Up until 1.1, you should have used $this->response. Return values were ignored. As of 1.1, you should use a return value (anything that can be cast as string), and $this->response is deprecated. This means you can still use $this->response, so your pre-1.1 code isn't broken, but you'll see a warning in the logs about the fact that you're using deprecated code. If you use both, you're doing it wrong. It's either one or the other. And as of the next version, $this->response will be completely removed. So it should not be used anymore in any bit of code you write now. The reference to $response has already been removed from the development documentation. If you insist on using $this->response in your controllers, use
    $this->response = \Response::forge();
    
    in your controller constructor, and return $this->response in your action methods. This way you can still do whatever you want to do now, and still have a working controller when you upgrade to the next FuelPHP version. I've added some clarification to the documentation as to the use of the return value in Controller action methods.
  • Up until 1.1, you should have used $this->response. Return values were ignored. As of 1.1, you should use a return value (anything that can be cast as string), and $this->response is deprecated. This means you can still use $this->response, so your pre-1.1 code isn't broken, but you'll see a warning in the logs about the fact that you're using deprecated code. If you use both, you're doing it wrong. It's either one or the other. And as of the next version, $this->response will be completely removed. So it should not be used anymore in any bit of code you write now. The reference to $response has already been removed from the development documentation. If you insist on using $this->response in your controllers, use
    $this->response = \Response::forge();
    
    in your controller constructor, and return $this->response in your action methods. This way you can still do whatever you want to do now, and still have a working controller when you upgrade to the next FuelPHP version. I've added some clarification to the documentation as to the use of the return value in Controller action methods.

    1. I fully agree, it just not obvious from docs where controllers are written for 1.1 version and response for 1.0. Doing 1 + 2 = incorrect usage ( such use is not logged as deprecated ). 2. I'm not insisting to use old style response. Returning "strings" (I prefer \Views) is much useful and natural, but I don't see reasons, for now, why should request object be manually created in action. 3. That is great, thank you. It would be nice if You added small notice to the Response section. P.S. message iis not well formed if use multiple quoting (
  • As I added to the documentation yesterday;
    A controller action should return it's result. The result can be a string, or anything that can be cast to string, like for example a View object. Alternatively you can return a Response object. You use a Response object primarily if you need to set special HTTP headers, or a custom HTTP status code ( a code other than "200 OK" ).

    We are aware of the issues with this forum, we're going to switch software soon.

Howdy, Stranger!

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

In this Discussion