Download the PHP package fracture/http without Composer
On this page you can find all versions of the php package fracture/http. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download fracture/http
More information about fracture/http
Files in fracture/http
Package http
Short Description HTTP abstraction library, extracted dependency of fracture/router.
License BSD
Homepage https://github.com/fracture/http
Informations about the package http
Fracture\Http
Introduction
A simple abstraction for handling the HTTP requests and responses. Library is made for interacting with fracture\routing and provides simple object-oriented interface.
Installation
You can add the library to your project using composer with following command:
Usage
All of the following code will assume that the Composer's autoloader has already been included.
Basic request initialization
While initializing a new Request
instance manually is possible, for said instance to be fully prepared, it require several additional steps. To simplify this process, you should use RequestBuider
, which will perform all of those steps:
Use of this code fragment is sufficient for any basic website and will produces a ready-to-use Request
instance.
Requests and REST
When creating a site, that provides REST API, a common practice is to implement API versioning via HTTP Accept and Content-Type headers. But, if you send a POST
query with custom Content-Type header, PHP will not populate $_POST
with the information, that you sent to the server. And there are no $_PUT
and $_DETETE
superglobals in PHP.
To retrieve data information in a usable form, you have define a content parser, which, if the media type matches, is executed to supplement the Request
instance with additional parameters.
It is possible for RequestBuilder
instance to have multiple content parsers added.
Content parsers
A content parser is defined as an anonymous function, which will be executed with Fracture\Http\Headers\ContentType
and Fracture\Http\Request
instances as parameters and is expected to return an array of name => value
pairs for parameters.
You can also use content parsers to override Request
attributes. For example, if you want to alter the request method, when submitting for with "magic" parameter like <input type="hidden" name="_my_method" value="PUT" />
(which is a common approach for making more RESTful and bypass the limitations of standard webpage):
Accessing data in the request
When the instance of Request
has been fully initialized (preferably, using RequestBuilder
), you gain ability to extract several types of information from this abstraction.
Parameters
To retrieve a parameter from an initialized Request
instance you have to use getParameter()
method:
When instance has been produced using RequestBuilder
, it will contain parameter values from $_GET
, $_POST
and the data from content parsers.
Important!
If your code contains two parameters in$_GET
and$_POST
with same name, it will trigger a warning. Same warning will also be triggered, if one of the content parsers returned parameter, which already existed in$_GET
or$_POST
. The library is designed with an assumption, that having such overlap is an unintentional mistake.
The getParameter()
method has the following signature:
If parameter with the provided name does not exist, the method will return null
.
Cookies
Retrieval of cookies is done using getCookies()
method.
The getCookie()
method has the following signature:
If parameter with the provided name does not exist, the method will return null
.
File uploads
The method for retrieving uploaded files from Request
instance is getUpload()
:
This method is called with the input's name as parameter. Depending on the structure of your upload form, it will return either an instance of UploadedFile
, if your HTML had name="attachment"
, or FileBag
instance, if form element had name="attachment[]"
.
If no file has been uploaded with the given field name, then the method will return null
.
The method signature is:
Request method
To retrieve the HTTP request method, you have to use getMethod()
method.
It has the following signature:
Headers
Currently the Request
instance lets you access abstractions for Accept and Content-Type HTTP headers. It can be done using getAcceptHeader()
and getContentTypeHeader()
methods. These methods have the following signatures:
and