benjaminvr

PHP - Router

Jul 28th, 2021
279
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 0.73 KB | None | 0 0
  1. <?php
  2.  
  3. class Router {
  4.     public $url_array;
  5.     public $arguments;
  6.  
  7.     public function __construct(){
  8.         $this->url_array = parse_url($_SERVER['REQUEST_URI'])['path'];
  9.         $this->arguments = array_filter(explode("/", $this->url_array));
  10.         $this->includeNecessaryFile();
  11.     }
  12.  
  13.     public function includeNecessaryFile(){
  14.     if(count($this->arguments) > 0){
  15.         if(file_exists("main/".$this->arguments[1].".php") && $this->arguments[1] !== 'index'){
  16.             require("main/".$this->arguments[1].".php");
  17.             // any excess arguments are stored in Router.arguments and can be accessed by the included file using $this
  18.         }
  19.     }   else {
  20.             require("main/index.php");
  21.         }
  22.     }
  23. }
  24.  
  25.  
  26. $index_router = new Router();
Advertisement
Add Comment
Please, Sign In to add comment