/* Here we present the simplest routing with little automation and easy extensibility for all projects tipes. you can grab full example on my git hub https://github.com/fen1979/routingExample feel free to use it or improve it */ // this file Routing.php class Routing{ private array $pages = array(); // saving parameters for redirection public function addRout($url, $path) { $this->pages[$url] = $path; } // doing good :) maybee public function route($url) { $fileDir = $this->pages[$url]; // if route path empty if (empty($fileDir)) { require 'public/404.php'; die(); } // if file exist if (file_exists($fileDir)) { // view this page require $fileDir; } else { // if route not empty but file not exist require 'public/404.php'; die(); } } // getting request parameters POST, GET no matter public function getUrl(): string { $arr = explode('?', $_SERVER['REQUEST_URI']); $url = $val = ''; if (empty($arr)) { // if addres contains two slashes like /home/ // this scenario prevent going to folder with same name $url = (count_chars($_SERVER['REQUEST_URI'], 1)[47] > 1) ? rtrim($_SERVER['REQUEST_URI'], '/') : $_SERVER['REQUEST_URI']; } else { // if request not contains two slashes // or contains some data or anything alse $url = (count_chars($arr[0], 1)[47] > 1) ? rtrim($arr[0], '/') : $arr[0]; // optional variable contains parameters sended in request // NOTICE!!! check if exist in $arr[1] before use it // this val can be used only on index.php!!! // most time this value isn't need at ol // $vall = $arr[1]; } return $url; } } // ---------------------------------------------------- // this file index.php // require to use class Routing require 'Routing.php'; // creating new instance of Routing class $r = new Routing(); $r->addRout('/', 'views/login.php'); // set the url to route like this $r->addRout('/home', 'views/home.php'); // exampe about page etc... $r->addRout('/about', 'views/about/about.php'); // Here you can add any number of pages and transitions... // getting url POST, GET no matter $url = $r->getUrl(); // call the routing function once to view page source $r->route($url); // you can minify this call to // $r->route($r->getUrl() ?? '/') // ==================================================== // /* DESCRIPTION The structure of the project folders and following the MVC patterns are not important, this example is expandable for any structural solutions, the example shows a partial implementation of the MVC pattern. The main condition for the functionality of the example is a single entry point into the project index.php, configured for nginx in the server settings, for Apache in the .htaccess file. $_GET[] with some parameters: When requests are displayed in the address bar and the parameters are displayed there, the parameters are used as in a regular get request, below is an example of accessing the about us page through <a> button indicating the values on your page : - press tis button you go to about page <a href="/about">About Us</a> - press this button about page hase be open in new tab <a href="/about" target="_blank" rel="nofollow" >About Us</a> Get request in file .php if(isset($_GET["some-name"])){ // do some actions } - for request from form the action need to be empty if your request going - to self page scripts <form action="" method="get"> // some inputs... </form> - else if your request go to another page <form action="/about" method="get"> // some inputs... </form> the same actions fo form when method equal POST $_POST[] with some parameters: When submitting a form using the post method on the page with the form, leave the ACTION="" attribute in the form need to be empty!! When sending a form to another page with a transition to this page, enter the route for this page in the action attribute of the form <form action="/" method="post"> // here we goin to Login.php // some inputs... </form> - receiving data from a post request in a login.php script file: if(isset($_POST["some-name])){ // do some actions // redirection to needed page after some actions header("Location: /home"); exit(); } */ <?php $request = $_SERVER['REQUEST_URI']; switch ($request) { case '/' : require __DIR__ . '/views/index.php'; break; case '' : require __DIR__ . '/views/index.php'; break; case '/about' : require __DIR__ . '/views/about.php'; break; default: http_response_code(404); require __DIR__ . '/views/404.php'; break; }