PHP Classes

File: index.php

Recommend this page to a friend!
  Classes of Adrian M   PHP Routing without a Framework   index.php   Download  
File: index.php
Role: Example script
Content type: text/plain
Description: Example script
Class: PHP Routing without a Framework
Register functions to handle HTTP requests by path
Author: By
Last change:
Date: 1 year ago
Size: 609 bytes
 

Contents

Class file image Download
<?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']);