Cygnite - A Modern Toolkit For Web Developers

The Elegant Way of Building Full-Featured Web Applications

Cygnite Dynamic Routing

Responses


Documentation


HTTP Responses

Introduction

All controllers and routes should return a response object back to user browser. The Response object holds the information which needs to return to browser. You can set the response content, HTTP status code, array of headers for building HTTP responses.

Response Object

Every controller's action and routes should return a Response object. However if you just return a string from your routes it will be converted into an HTTP response by the framework.


 $app->router->get('hello/', function () 
 {
     return "Hello World!";
 });
 


 use Cygnite\Http\Responses\Response;
 use Cygnite\Http\Responses\ResponseHeader;

 $app->router->get('home/about', function () 
 {
     return Response::make('Content' , ResponseHeader::HTTP_OK, ['Content-Type', 'text/html']);
 });
 

Simply return Response object from your controller action.


 use Cygnite\Http\Responses\Response;
 use Cygnite\Http\Responses\ResponseHeader;

 public function indexAction()
 {
    
    return Response::make("Content");
 }

Add Headers To Responses

You may add headers to your response before return back to the browser. You can use setHeader method to attach multiple or single header to your responses.


 use Cygnite\Http\Responses\Response;

 return Response::make('Content')->setHeader('Content-Type', 'application/html');


Set multiple headers to your response.


 use Cygnite\Http\Responses\Response;

 return Response::make('Content')->setHeader([
          'Content-Type' => 'application/json',
          'X-Powered-By' => 'Cygnite v2.x',
          'X-Header-One' => 'Header Value'
      ]); 


Setting Status Code & Charset

By default, a status code of 200 (HTTP OK) is set for all response. You can also set status code, charset etc. over the response object.


 use Cygnite\Http\Responses\Response;
 use Cygnite\Http\Responses\ResponseHeader;

 return Response::make('Content', ResponseHeader::HTTP_OK); 

 return Response::make('Content')->setStatusCode(ResponseHeader::HTTP_NOT_FOUND); 


For other response code and status message are defined in Cygnite\Http\Responses\ResponseHeader class. You may use as needed.


 use Cygnite\Http\Responses\Response;

 return Response::make('Content');

 return Response::make('View Content')->setCharset('ISO-8859-1');


Creating JSON Responses

You may use json method on Response object which will set the Content-Type header to application/json, and convert the given array as json object using native php json_encode function.


 use Cygnite\Http\Responses\Response;

 return Response::json(['name' => 'Shane', 'country' => 'USA', 'state' => 'Alaska']); 


Streaming A Response

The Cygnite\Http\Responses\StreamedResponse class allows you to stream the Response back to the browser. The response content must be php callable instead of string.


 use Cygnite\Http\Responses\StreamedResponse;

 return new StreamedResponse(function () {
    echo 'Hello World';
    flush();
 });

 // or

 use Cygnite\Http\Responses\Response;

 return Response::streamed(function () {
    // .. 
 });


You may also add headers to stream response.


Follow Us On Facebook Twitter Google+ Linkedin
Released Under The MIT Public License. Copyrights @2012-2017. Powered by- Sanjoy Dey Productions.