In this page:
While PHP offers
echo
and
print
for sending output, WebFiori's
webfiori\http\Response
class provides a centralized approach. It gathers server output for streamlined delivery to clients after request processing. Additionally, it allows for sending custom HTTP headers. This empowers developers to construct well-structured and efficient responses within their WebFiori applications.
Note : This class is part of the library
webfiori\http
.
Collecting server output can be archived using the static method
Response::write()
. From its name, we can infer that it is used to write a string to response body.
Note: In most cases, the developer will not have to collect the output him self. If the class
WebPage
is used for rendering HTML, no need to use it. This also applies to web services if the classWebServicesManager
orExtendedWebServicesManager
is used to manage your web APIs.
Code
namespace app\ini\routes;
use webfiori\http\Response;
use webfiori\framework\router\Router;
use webfiori\framework\router\RouteOption;
class ClosureRoutes {
public static function create() {
Router::closure([
RouteOption::PATH => '/closure',
RouteOption::TO => function() {
Response::write('Hello,<br/>');
Response::write('Welcome to my website!<br/>');
Response::write('Current Time is: '.date('H:i:s'));
}
]);
}
}
It is possible to terminate code execution on server before the whole code is executed. This can be archived by manually invoking the static method
Response::send()
. This can happen if the developer would like to debug his code and checking the value of specific variable.
Code
namespace app\ini\routes;
use webfiori\http\Response;
use webfiori\framework\router\Router;
use webfiori\framework\router\RouteOption;
class ClosureRoutes {
public static function create() {
Router::closure([
RouteOption::PATH => '/closure',
RouteOption::TO => function() {
$myVar = 'Good;
Response::write($myVar);
$myVar = 'Hello';
Response::write($myVar);
// This will terminate code execution
Response::send();
// This will show the value of $myVal twice.
}
]);
}
}
The developer can use the method
Response::addHeader()
to add custom headers to the response. For example, we can send a JSON string and set its content type to
application/json
.
Code
namespace app\ini\routes;
use webfiori\http\Response;
use webfiori\framework\router\Router;
use webfiori\framework\router\RouteOption;
class ClosureRoutes {
public static function create() {
Router::closure([
RouteOption::PATH => '/closure',
RouteOption::TO => function() {
$jsonData = '{"username":"WarriorVx", "email":"my-email@example.com", "age":33}';
Response::write($jsonData);
Response::addHeader('content-type', 'application/json');
}
]);
}
}
In simple terms,
HTTP Cookies
are small piece of text which is set by the server for session management, personalizing content or tracking. Cookies are set by sending HTTP header
set-cookie
. At minimum, a cookie must have a name and value.
Cookies have multiple attributes that must be set on order to have it valid like duration, path and others. Cookies in WebFiori are represented by the class
HttpCookie
In order to send a new cookie, an object of type
HttpCookie
is created and then added to the response using the method
Response::addCookie(HttpCookie)
as follows:
Code
namespace app\ini\routes;
use webfiori\http\Response;
use webfiori\http\HttpCookie;
use webfiori\framework\router\Router;
use webfiori\framework\router\RouteOption;
class ClosureRoutes {
public static function create() {
Router::closure([
RouteOption::PATH => '/closure',
RouteOption::TO => function() {
$cookie = new HttpCookie();
$cookie->setName('hello-cookie');
$cookie->setValue('hello-world');
Response::addCookie($cookie);
}
]);
}
}
By default, the class
Response
will send any output with code
200 - Ok
. But it is possible to send the response using different response code. The method
Response::setCode()
can be used to achieve this.
Code
namespace app\ini\routes;
use webfiori\http\Response;
use webfiori\framework\router\Router;
use webfiori\framework\router\RouteOption;
class ClosureRoutes {
public static function create() {
Router::closure([
RouteOption::PATH => '/closure',
RouteOption::TO => function() {
Response::write('Resource Was Not Found!');
Response::setCode(404);
}
]);
}
}
For some reason or another, the developer may want to clear the collected server output and replace it with another. Also, he might want to clear the headers. Clearing all output can be performed using the method
Response::clear()
. To clear response body only, the method
Response::clearBody()
can be used. To clear headers only, the method
Response::clearHeaders()
can be used.
Code
namespace app/ini/routes;
use webfiori\http\Response;
use webfiori\framework\router\Router;
use webfiori\framework\router\RouteOption;
class ClosureRoutes {
/**
* Create all closure routes. Include your own here.
* @since 1.0
*/
public static function create() {
Router::closure([
RouteOption::PATH => '/closure',
RouteOption::TO => function() {
Response::append('Resource Was Not Found!');
Response::setCode(404);
Response::clear();
Response::write('Resource Was Found!');
Response::setCode(200);
//This will send back "Resource Was Found!"
}
]);
}
}
Next: UI Package
Previous: Routing