Table of Contents

Requests (HTTP)

Requests are not a separate component, but are part of a larger component which provides specific input / output components for your application. The most common request is HTTP requests. This is what is documented here.

Requests are designed for both receiving (from clients as a server) and sending (as client to a server).

Installation

composer require dotink/inkwell-http

You do not need to explicitly install this package if you're using the inKWell routing package.

Instantiation

Although you can instantiate an empty request and work with it directly, in most cases you're going to want your request to be populated from the data provided by the SAPI. In order to do this you can create a gateway server and populate the request.

use Inkwell\HTTP;

$request = new HTTP\Resource\Request();
$gateway = new HTTP\Gateway\Server();

$gateway->populate($request);

Getting / Setting

There are a number of properties you can get/set on the request itself or on components which are directly relatedo the request.

URLs

The URL is held in a URL object which provides a number of additional methods, see: https://github.com/dotink/flourish-url. To get the URL object you need to use the getURL() method:

$url = $this->request->getURL();

Additionally there are methods on the URL object for retrieving separate pieces:

$host = $this->request->getURL()->getHost();
$path = $this->request->getURL()->getPath();

You can set partial aspects of the URL, thereby modifying it:

$this->request->setURL('/new/path.html');

HTTP Method

Get the method:

$method = $request->getMethod();

Check the method:

use IW\HTTP;

if ($request->checkMethod(HTTP\POST)) {
	//
	// Do some posting
	//
}

Set the method:

use IW\HTTP;

$request->setMethod(HTTP\GET);

HTTP Headers

You can get or set headers on a request by working with the headers property which is populated as an instance of Dotink\Flourish\Collection, see: https://github.com/dotink/flourish-collection.

$accept = $this->request->headers->get('Accept');

You can get all headers as an array via:

$headers = $this->request->headers->get()

Set a specific header by doing:

$this->request->headers->set('X-Forwarded-For', $ip_address);

Set multiple headers with an array:

$this->request->headers->set([
	'Accept-Language' => $this->request->cookies->get('lang'),
	'X-Custom-Header' => $value
]);

Parameter Data

Get data:

$name = $this->request->params->get('name');

Provide a default:

$page = $this->request->params->get('page', 1);

Get nested data:

$name = $this->request->params->get('person.name', 'Jesse Doe');

Get all the data:

$params = $this->request->params->get();

Set data:

$this->request->params->set('task', $next_task->getId());

Set nested data:

$this->request->params->set('person.age', 30);

Cookies

Get a cookie value:

$user_token = $this->request->cookies->get('user_token', NULL);

Files

Get a file:

$document = $this->request->files->get('document');

Get a nested file:

$profile_image = $this->request->files->get('user.profile_image');

Discussion