Cygnite - A Modern Toolkit For Web Developers

The Elegant Way of Building Full-Featured Web Applications

Cygnite Dynamic Routing

Requests


Documentation


HTTP Requests

Introduction

Interacting with HTTP requests and response very easy. Cygnite provides a object-oriented wrapper for PHP super global variables. HTTP request object created from $_GET, $_POST, $_SERVER, $_COOKIE, $_FILES, $_ENV global variables.

Creating A Requests

You may create a request using createFromGlobals() method, it is equivalent of calling __construct() method.


 use Cygnite\Http\Requests\Request;

 $request = Request::createFromGlobals();

 or 

 $request = new Request($_GET, $_POST, $_COOKIE, $_SERVER, $_FILES, $_ENV, $content = "");


Accessing Request Data

Request object stored in Ioc container. You may access the current HTTP request object in your controller action using the application object:


 public function profileAction($id)
 {
    $app = $this->app();
    $request = $app->request;

    // profile/1/?foo=bar 
    echo $request->input('foo'); // output: bar
 }

The Request object holds the information of current request. You may access request information via properties:

Accessing Query String Data


 $request->query->all(); // retrieving all query string data

 $request->query->get("foo");

Note: This is similar to calling $_GET and $request->query returns Collection object.

Accessing Post Data


 $request->post->all(); // retrieving all post data

 $request->post->get("foo"); // specific post data

Note: This is similar to calling $_POST and $request->post returns Collection object.

Accessing Request Data


 $request->input("foo");

Note: This is similar to calling $_REQUEST. You can use when you are not aware about the form of request, whether query string, post, delete, put, or patch data. If request is JSON it will return the value from JSON object.

Accessing Server Data


 $request->server->get("foo");

Note: This is similar to calling $_SERVER.

Accessing Header Data


 $request->header->get("Content-Type");

Note: This are special $_SERVER values with keys prefixed with "HTTP_", $request->header/ $request->getHeader() returns a Collection object.

Accessing ENV Data


 $request->env->get("foo");

Note: This is similar to calling $_ENV.

Accessing FILES Data


 $request->file->get("foo");

Note: This is similar to calling $_FILES.

Accessing Put, Patch, Delete Data

Unlike other methods put, patch, delete property returns Collection object, but difference is it returns with x-www-form-urlencoded content type.


 $request->put->get("foo");

 $request->patch->get("foo");

 $request->delete->get("foo");

Collections

All above request data properties are wrapped into \Cygnite\Foundation\Collection object. Which is the data holder class. Below are mostly used methods over all global properties:

set($key, $value),
get($key),
has($key),
remove($key)

Read more about Collection class.

Getting the Method

Determine which HTTP method ($_GET or $_POST) used for the request using getMethod() :


 $request->getMethod();
 

HTTP Methods Spoofing

Generally HTML forms allows only GET or POST methods. However you you can spoof other methods by adding a hidden input field with the name "_method" and the value should be the HTTP method which you would like to spoof. For an example:


 <form method="POST">
    <input type="hidden" name="_method" value="DELETE">
    ...
 </form> 

Alternatively you can use X-HTTP-METHOD-OVERRIDE header to set the HTTP method.

Simulating a Request

You can also simulate the request from given URL instead creating request from PHP globals.


 $request->create('/hello-world' , 'GET', ['name' => 'Sanjoy']);

Request Body

The request body render from the php://input stream.

Getting Raw Body


 $request->getRawBody();

Checking If Request Is JSON


 $request->isJson();

Getting JSON Body


 $request->getJsonBody();

Determining Is Ajax Request


 $request->isAjax();

Checking If HTTPS


 $request->isSecure();

Getting The Client IP Address


 $request->getClientIPAddress();

Getting Full URL


 $request->getFullUrl();

Getting Referrer URL


 $request->getReferrerUrl();

Getting Path Info


 $request->getPath();

Getting Authentication Information



 $request->getUser();
 $request->getPassword();


Setting Trusted Proxies


 use Cygnite\Http\Requests\Request;

 Request::setTrustedProxies(["192.168.127.71", "192.168.128.91"]);


You may also add trusted proxy headers.


 use Cygnite\Http\Requests\Request;
 use Cygnite\Http\Requests\RequestHeaderConstants;

 Request::setTrustedHeaderName(RequestHeaderConstants::CLIENT_HOST, "X-Proxy-Ip");



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