/*
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;
}

PHP相关代码片段

elementor filter posts dynamically by author

tiktok login scope_not_authorized laravel

convert to webp laravel

php/Phone validierungstyp

laravel create enum migration

Guzzle withBasicAuth

Laravel macro

what is polymorphic relationship in laravel

php mac change

spatie validate name unique

laravel route subfolder

laravel artisan create command

symfony entity target entity

php two spase remove

mailtrap laravel

php check if var is datetime

bulk taxonomy addition wordpress php

install laravel 10

php datetime format milliseconds

PRAGMA foreign_keys

PHP function to sanitize input prevent XSS

curl php get

php array to stdclass

php curl get parameters array

curl php no response

fpdf allow special characters

stripe payment in php

php filter associative array

Laravel 11 - Production assets issue

php get const properties of a class

http_build_query bearer token

Why do Sessions disappear on refresh? - Laravel

hooks in codeigniter

hard delete laravel

how to view the data in json

laravel pagination with relationship

custom pagination laravel

laravel add new command provider

Laravel Request validation Multiple column Unique

php artisan publish stub

old and edit value laravel blade

how to var_dump in php

how to change php version in ubuntu

temporary change php version to 7.4 ubuntu

terminal in php

get all the variables defined in a php file

is csrf token fixed for a laravel website?

php new datetime

Print loop data in a sigle cell in laravel excel

array to string laravel

joomla get custom fields

time ago function

woocommerce get product by title

what differene insert save and create in laravel

Custom error auth massage laravel

convert letters to numbers php

convert number to letter php

php url rewrite

tcpdf table page break border bottom

laravel View path not found.

php how to check if datetime is valid

php curl and https

ping website is working php

scoped query laravel

php get fractional part of float

laravel scope filter relationship

laravel return empty 200 response

php version required for laravel 11

@selected blade directive

nested back buttons laravel site:stackoverflow.com

Laravel API Resources

set laragon terminal in vscode

pingdom plugin for php

php doc block

php doc block array object

laravel network run

Save dataimage format to save laravel

Illuminate\Queue\MaxAttemptsExceededException

php array print

laravel upi validation