In this page:
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.
Consider a scenario where you have an HTML file named
index.html
within the folder
[APP_DIR]/pages
, containing the following content:
Code
<!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
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.
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.
Imagine a PHP file named
SayHi.php
residing within the
[APP_DIR]/pages
directory, containing the following code
Code
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
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
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
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.
Assuming that the following PHP class represent a dynamic web page:
Code
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
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