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".

Basic Usage | WebFiori
WebFioriAPI ReferenceLearnDownloadContributeLearn{{result.parent_page}} > {{result.title}}{{result.title}}Classes{{result.class_name}}{{result.summary}}Methods{{result.name}}{{result.summary}}

Basic Usage

In this page:

Introduction

WebFiori Framework empowers developers to construct web applications with exceptional efficiency. It excels at serving web pages, which can be either static or dynamic in nature. The framework employs routing, a mechanism that intelligently directs incoming requests to the appropriate resources within your application. By default, web pages reside within the [APP_DIR]/pages directory, although the framework grants flexibility to store them in custom locations if desired.

Serving Static Pages

Consider a scenario where you have an HTML file named index.html within the folder [APP_DIR]/pages, containing the following content:

Code

12345678910111213141516171819
<!DOCTYPE html>
<html>
    <head>
        <title>Hello</title>
        <meta charset="UTF-8">
    </head>
    <body>
        <div>
            <p>
                Welcome to My Home Page
            </p>
            <p>
                This page is served using 
                <a href="https://webfiori.com/webfiori">WebFiori Framework</a>
            </p>
        </div>
    </body>
</html>

To display this page to users, you'll need to establish a route that maps the file's location. Assuming the file is situated within the [APP_DIR]/pages directory, you can utilize the Router::page() method provided by the webfiori\framework\router\Router class. The ViewRoutes class, located at [APP_DIR]\ini\routes\ViewRoutes.php, offers a static method specifically designed to create routes for pages. Here's the code demonstrating how to create a route for this static page:

Code

12345678910111213141516
namespace app\ini\routes;

use webfiori\framework\router\Router;
use webfiori\framework\router\RouteOption;

class PagesRoutes {

    public static function create() {

        Router::page([
            RouteOption::PATH => '/home', 
            RouteOption::TO => '/index.html'
        ]);
    }
}

Presuming the base URL of your website is https://example.com, navigating to https://example.com/home should successfully render the created page within the web browser.

Serving Dynamic Pages

Dynamic pages offer an enhanced level of interactivity compared to static pages. They can incorporate executable PHP code or be represented by classes with corresponding routes established within your application.

Routing to PHP Files

Imagine a PHP file named SayHi.php residing within the [APP_DIR]/pages directory, containing the following code

Code

12345678910
namespace app\pages;

use webfiori\http\Response;

class SayHi {
    public function __construct() {
        Response::write('Hi Visitor. Welcome to my website!');
    }
}

The webfiori\http\Response class plays a crucial role in managing server responses. It primarily functions by accumulating output generated by your application logic and sending it back to the client. The Response::write() method is employed to append output to the response object. To delve deeper into the functionalities of this class, kindly refer to this documentation.

Assuming that a route to the page is created as follows:

Code

123456789101112131415
namespace app\ini\routes;

use webfiori\framework\router\Router;
use webfiori\framework\router\RouteOption;

class PagesRoutes {

    public static function create() {
        Router::page([
            RouteOption::PATH => '/say-hi', 
            RouteOption::TO => '/SayHi.php'
        ]);
    }
}

Navigating to http://example.com/say-hi in your web browser should display the message "Hi Visitor. Welcome to my website!". We can further elevate this example by personalizing the greeting. Instead of a generic "Hi Visitor," we can leverage parameters within the URL path itself. A parameter, denoted by curly braces (e.g., {user-name}), represents a segment of the path that can hold any value provided by the user. The code for creating a route with a parameter becomes:

Code

123456789101112131415
namespace app\ini\routes;

use webfiori\framework\router\Router;
use webfiori\framework\router\RouteOption;

class PagesRoutes {

    public static function create() {
        Router::page([
            RouteOption::PATH => '/say-hi/{person-name}', 
            RouteOption::TO => '/SayHi.php'
        ]);
    }
}

Accessing the value of the variable inside the class SayHi can be performed by using the method Router::getParameterValue() as follows:

Code

123456789101112
namespace app\pages;

use webfiori\framework\router\Router;
use webfiori\http\Response;

class SayHi {
    public function __construct() {
        $personName = Router::getParameterValue('person-name');
        Response::write('Hi '.$personName.'. Welcome to my website!');
    }
}

Now when navigating to https://example.com/say-hi/Ibrahim BinAlshikh, the message Hi Ibrahim BinAlshikh. Welcome to my website! will appear. The string Ibrahim BinAlshikh can be replaced by anything, and it will say "hi" to it.

Routing to PHP Classes

Assuming that the following PHP class represent a dynamic web page:

Code

123456789101112
namespace app\pages;

use webfiori\framework\router\Router;
use webfiori\http\Response;

class SayHi {
    public function __construct() {
        $personName = Router::getParameterValue('person-name');
        Response::append('Hi '.$personName.'. Welcome to my website!');
    }
}

It is possible to have a route which points to the class directly as follows:

Code

123456789101112131415
namespace app\ini\routes;

use webfiori\framework\router\Router;
use webfiori\framework\router\RouteOption;

class PagesRoutes {

    public static function create() {
        Router::addRoute([
            RouteOption::PATH => '/say-hi/{person-name}', 
            RouteOption::TO => \app\pages\SayHi::class
        ]);
    }
}

Next: Routing

Previous: Folder Structure

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