PHP Classes

PHP Routing without a Framework: Register functions to handle HTTP requests by path

Recommend this page to a friend!
  Info   View files Example   View files View files (9)   DownloadInstall with Composer Download .zip   Reputation   Support forum (1)   Blog    
Last Updated Ratings Unique User Downloads Download Rankings
2023-11-04 (7 months ago) RSS 2.0 feedNot enough user ratingsTotal: 110 All time: 9,632 This week: 65Up
Version License PHP version Categories
routerclass 1.0Freeware7.4HTTP, Design Patterns, PHP 7
Description 

Author

This package can register functions to handle HTTP requests by path.

It can register one or more callback functions that will handle HTTP requests of given request URL paths.

The package can also process the current request and call the respective callback function that was registered to handle the requests for the current HTTP request URL path.

If the paths of any of the registered callback functions matches the URL path of the current HTTP request, the package will load the script pages/404.php .

Picture of Adrian M
  Performance   Level  
Name: Adrian M <contact>
Classes: 4 packages by
Country: Ireland Ireland

Instructions

The PHP Routing without a Framework package aims to support new versions of PHP.

Currently, it requires PHP 7.4 or a more recent version.

Example

<?php
error_reporting
(1);
// Include the router file
require_once 'router.php';

// Initialize the router
$router = new Router();

// Define routes and associate them with page names
$router->addRoute('/', 'home', function () {
   
// Code for the Home page
   
include 'pages/home.php';
});

$router->addRoute('/about', 'about', function () {
   
// Code for the About page
   
include 'pages/about.php';
});

$router->addRoute('/contact', 'contact', function () {
   
// Code for the Contact page
   
include 'pages/contact.php';
});

// Handle the current request
$router->handle($_SERVER['REQUEST_URI']);


Details

Router

Simple PHP Router


  Files folder image Files  
File Role Description
Files folder imagepages (4 files)
Accessible without login Plain text file .htaccess Data Auxiliary data
Accessible without login Plain text file index.php Example Example script
Accessible without login Plain text file LICENSE Lic. License text
Accessible without login Plain text file README.md Doc. Documentation
Accessible without login Plain text file Router.php Class Class source

  Files folder image Files  /  pages  
File Role Description
  Accessible without login Plain text file 404.php Aux. Auxiliary script
  Accessible without login Plain text file about.php Aux. Auxiliary script
  Accessible without login Plain text file contact.php Aux. Auxiliary script
  Accessible without login Plain text file home.php Aux. Auxiliary script

 Version Control Unique User Downloads Download Rankings  
 100%
Total:110
This week:0
All time:9,632
This week:65Up