简体   繁体   中英

Recess Framework + Adding Header

How can I add an HTTP Header on Recess Framework.

I try this one.

header('HTTP/1.1 204 No Content');

but it's not working.

Thanks.

In this case, the best way to add an HTTP header for a particular response is to create a new Response Type. This will let you return from controller methods in a normal manner (like return $this->ok()) and have the framework add the header. I bet your version of Recess has the No Content response, but its not in the AbstractController so you aren't able to access it. Follow these steps to check.

Look in the recess/ directory. We will be modifying several files.

First, create the recess/http/responses/NoContentResponse.class.php file, if it doesn't already exist(it may very well exist in your version). Use this code:

<?php
Library::import('recess.http.Response');
Library::import('recess.http.ResponseCodes');

class NoContentResponse extends Response {
    public function __construct(Request $request) {
    parent::__construct($request, ResponseCodes::HTTP_NO_CONTENT, 
                 ResponseCodes::getMessageForCode(ResponseCodes::HTTP_NO_CONTENT));
    }
}
?>

Make sure the HTTP_NO_CONTENT response code is in the recess/http/ResponseCodes.class.php file(it should already be in there).

Now you will want to make sure that the response is accessible from your controllers. Add the following code to the recess/framework/AbstractController.class.php file:

protected function noContent() {
    Library::import('recess.http.responses.NoContentResponse');
    $response = new NoContentResponse($this->request);
    return $response;
}

This will let you return $this->noContent() from your controllers just like any other response. Good luck!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM