Skip to main content

Static Routing

Static routing is a simple method of routing in web development where the URL paths are predefined and directly mapped to specific content or functionality. Unlike dynamic routing, where routes are generated based on certain conditions or parameters, static routes remain constant and do not change unless explicitly modified by the developer.

Using Static Routes with Phlame

Phlame provides a built-in Routing Class, that can be use for creating routes easily.

Syntax of the Routing Functions

For adding a New Route you can use the addRoute method from the Router class. The syntax and an example of the addRoute method is

Syntax:
addRoute(<route>, <callback>);
Example:
addRoute('/page/url', function(){
// Code to be executed
});

The addRoute method sets the route, in the Router class's routers, the actions will be executed by the route functions int the Router class.

Example of a Phlame Static Route

The index.php page is serving as the entry point. It comes with a boilerplate code using the Phlame router class to create a static route:

index.php
<?php
session_start();
require_once '.config/_init.php';

$router = new Router();

// Add a static route
$router->addRoute('/', function() {
echo "hello, world!";
});

// More routes can be created

$router->route();
?>

This code snippet demonstrates how to use the addRoute() method from the Phlame router class to define a static route. In this example, the route / maps to the callback function that sets the content to be displayed from the index source file.

info

If duplicate routes are added, the first version of the route will be considered as the real route.