-
Notifications
You must be signed in to change notification settings - Fork 0
The RequestResponse Classes
Request handlers have to return a class that implements the
RequestResponse interface located in RequestResponse.php
Current there are few implementations inside Responses/.
If none of them suits your needs you're welcome to implement one
yourself or open an issue to share your use case if you consider
that we need another default implementation.
The RequestResponse interface requires:
- A constructor taking an HTTP response code,
200 OKby default. -
setHeader(string $key, string $value): voidproviding the functionality of php'sheader -
serve(): voidwhich is responsible for serving the request to the framework.
A RequestResponse has to provide all of the output processed by the
framework and has to do it only when serve() is called.
This is necessarry to provide the programmer with the ability to do mock calls to request handlers without affecting the output to the user.
You should also read:
Here are descriptions of the provided classes that implement
the RequestResponse interface:
This is not the correct way to implement RequestResponse
because there are side effects to the output even if serve() does
not get called.
This class implements RequestResponse in terms of it's usage. This is the wrong way to implement it, but also the simplest way to demonstrate what the methods in RequestResponse should do.
It also contains the extra echo() method, placed there to signify
that methods that the request handler should use to provide output
are not part of the RequestResponse interface.
The simplest way to properly implement the RequestResponse
interface.
This class simply buffers everything until serve() is called, as
this is the desired way a RequestResponse class should work like.
It also contains the getBuffer(): string and clearBuffer(): void
methods which allows the output from a request handler to be used
inside the application instead of getting served or ignored.
Note: In case you want to use a built-in method that outputs text
but you don't want to violate the no-side-effects-until-serve()
idea, you can read about PHP's
output control functions
A useful response for creating JSON API-s.
Permits response code modification.
Stores a single variable of any type that can be set with echo()
and retrieved with getOutput(). This is useful for reusing your
API handlers from your user interface request handlers.
When serve() is called, the stored variable is printed as
formatted JSON.
This class provides the means to use the Template system described in the Introduction section. Make sure to read that section before using it.
It takes both an HTTP status code and a template file as arguments.
As the RequestResponse interface forces the status code to be the
first argument and to be constructable without arguments
it is recommended to set these variables by name.
new TemplateResponse(file: "index.html");
new TemplateResponse(file: "forbidden.html", code: 403);Some interesting methods in the TemplateResponse class are:
setValue(string $name, string $value)setValues(array $keyValuePairs)getValue(string $name)