Request and Response
Introduction
Request and response are singletons. They can be injected to any class to retrieve request data or modifiy the output of the response.
Request
Rise\Request
contains methods for accessing data of the incoming request.
Rise\Request::getHttpVersion()
returns the HTTP version of the incoming request.
Rise\Request::getMethod()
returns the HTTP method of the incoming request.
Rise\Request::getPath()
returns the path of the request URL.
Rise\Request::getHost()
returns the host of the request URL.
Rise\Request::getHeader(string $key)
returns the value of a HTTP header.
Rise\Request::getContentType()
returns the content type of the request.
Rise\Request::getCharset()
returns the charset of the request.
Rise\Request::getQuery()
returns GET variables.
Rise\Request::getInput()
returns POST, PUT or DELETE variables.
Rise\Request::getUrlParams()
returns URL parameters defined in routes configurations.
Rise\Request::getUrlParam(string $key)
returns a URL parameter.
Rise\Request::getFiles()
returns uploaded files.
Response
Modes
There are three modes in response that will affect the usage of Rise\Response::send()
, they are 'STRING'
, 'FILE'
and 'STREAM'
.
'STRING'
is the default mode. It will send the body of the response once.
'FILE'
for sending file.
'STREAM'
for continuously sending content.
Rise\Response::setMode(string $mode)
will set the mode. e.g.
/* @var $response \Rise\Response */
$response->setMode(\Rise\Response::STRING);
$response->setMode(\Rise\Response::FILE);
$response->setMode(\Rise\Response::STREAM);
Send Response
Rise\Response::send($content)
will send the response to browser. It will send headers first, then it will send the body of the response.
In 'STRING'
mode, $content
will be the body of the response.
In 'FILE'
mode, $content
will be the path of the file.
In 'STREAM'
mode, $content
will be a chunk of the body. You can call Rise\Response::send($content)
multiple times to keep sending the message.
/* @var $response \Rise\Response */
// STRING mode
$response->setMode(\Rise\Response::STRING);
$response->send('Hello world'); // This will send "Hello world" to the browser.
$response->send('Good bye'); // This will not send anything to the browser.
// FILE mode
$response->setMode(\Rise\Response::FILE);
$response->send('/var/www/file'); // This will send the file "/var/www/file".
// STREAM mode
$response->setMode(\Rise\Response::STREAM);
$response->send('Hello');
$response->send(' world'); // It can send more than once.
For convenience, Rise\Response::sendFile(string $file)
can be used to send a file directly.
/* @var $response \Rise\Response */
$response->sendFile('/var/www/file');
// is equivalent to
$response->setMode(\Rise\Response::FILE)->send('/var/www/file');
Redirection
Rise\Response::redirect(string $url, int $statuCode = 302)
will send an HTTP redirection.
Rise\Response::redirectRoute(string $name, array $params = [], int $statusCode = 302)
will send an HTTP redirection with the URL generated by a named route.
Status Code
Rise\Response::setStatusCode(int $code)
will set the status code of the response. It should be called before Rise\Response::send()
.
Headers
Response headers can be updated before the response has been sent.
Rise\Response::hasHeader(string $name)
is used to check whether a header exists.
Rise\Response::getHeaders()
returns all headers.
Rise\Response::getHeader(string $name)
return the headers of a field name.
Rise\Response::setHeader(string $name, $values)
set a header.
Rise\Response::addHeader(string $name, $value)
add a header with the same field name.
Rise\Response::unsetHeader(string $name)
unset all headers of a field name.
Handlers
Handlers are the entry points configured in routing. Method injection is supported in handlers. To send a response in handler, we can inject Rise\Response
in the method. For example,
we have set a handler 'App\Handers\Home.index'
for our root. To make it send 'Hello world'
to the browser, we can change it to:
namespace App\Handlers;
use Rise\Response;
class Home {
public function index(Response $response) {
$response->send('Hello world');
}
}
Middlewares
Middlewares can be configured in routing. Just like handlers, method injection is supported in middlewares. The difference between middleware and handler is that a Closure
is injected into the method of a middleware and it is needed to be executed in order to run the next middleware or handler.
The simplest middleware will be like this, which do nothing but run the next middleware or handler.
use Closure;
class Middleware {
public function doNothing(Closure $next) {
$next();
}
}