Message (4)

Error: E_WARNING

Description: Run-time warning.

Message: Trying to access array offset on value of type null

Stack Trace:

#0 At webfiori\framework\ui\ErrorBox Line 614

#1 At webfiori\framework\WebFioriApp Line 193

#2 At webfiori\examples\views\MdPage Line 109

#3 At webfiori\examples\views\MdPage Line 44

#4 At app\ini\routes\PagesRoutes Line

#5 At Router Line 1436

#6 At webfiori\framework\router\Router Line 1540

#7 At webfiori\framework\router\Router Line 1398

#8 At webfiori\framework\router\Router Line 691

#9 At webfiori\framework\router\Router Line 56

#10 At webfiori\Index Line 83

#11 At webfiori\Index Line 87

Tip: To display more details about the error, define the constant "WF_VERBOSE" and set its value to "true" in the class "GlobalConstants".

The Class Response | WebFiori
WebFioriAPI ReferenceLearnDownloadContributeLearn{{result.parent_page}} > {{result.title}}{{result.title}}Classes{{result.class_name}}{{result.summary}}Methods{{result.name}}{{result.summary}}

The Class Response

In this page:

Introduction

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

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 class WebServicesManager or ExtendedWebServicesManager is used to manage your web APIs.

Code

12345678910111213141516171819
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'));
            }
        ]);
    }
}

Sending The Output

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

12345678910111213141516171819202122232425
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.
            }
        ]);
    }
}

Sending Custom Headers

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

12345678910111213141516171819
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');
            }
        ]);
    }
}

Working With HTTP Cookies

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

123456789101112131415161718192021
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);
            }
        ]);
    }
}

Custom Response Code

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

123456789101112131415161718
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);
            }
        ]);
    }
}

Clearing Output

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

123456789101112131415161718192021222324252627
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

View this page on GitHubLast modified: 1970-01-01 03:00:00
Powered By: WebFiori Framework, Vue and VuetifyAll Rights Reserved © 2018 - 2024