phpbackendcakephp-routing

Plain PHP router argument passing to views


I am creating a basic router in PHP, there is not much online material about how to write plain PHP router. So below is my current implementation. Basically every request is redirected to index.php and it then using class router redirects user to home.php or add.php. The question is how do I pass any type of variable from index.php to home.php or add.php?

<?php
//***********************
// Router class
//*********************
class router
{
  public $req = [];
  public $url = "";
  public $method = "";
  private $url_found = false;

  public function resolve($url)
  {
    $this->url = $url["REDIRECT_URL"];
    $this->request = $url;
    $this->method = $url["REQUEST_METHOD"];
  }

  public function get($route, $callback)
  {
    if ($this->method == "GET" && $this->url == $route && $this->url_found == false) {
      $url_found = true;
      $callback($this->req);
    }
  }

  public function post($route, $callback)
  {
    if ($this->method == "POST" && $this->url == $route && $this->url_found == false) {
      $url_found = true;
      $callback($this->req);
    }
  }
}



<?php
//*******************************
// index.php
//*********************************


require './router.php';

$app = new router();

$app -> resolve($_SERVER);

   $app -> get("/home", function ($req)
{
  require 'views/home.php';
});

$app -> get("/add", function ($req)
{
  require 'views/add.php';
});

$app -> post("/add", function ($req)
{
  require 'views/home.php';
});

My idea was that i need to save data I want to pass in some global variable and if I am requesting lets say "views/home.php" home.php will have access to that global variable and can use it.

Project file system:

+---php_router
|   |   .htaccess
|   |   index.php
|   |   notes.md
|   |   router.php
|   |
|   \---views
|           add.php
|           home.php

Solution

  • global variable will work because require is not redirect, but since you are in function scope you will have to namely list those global variables to grand access to them, but i'm not sure if thats good idea

    // index.php
    
    $var = 'global';
    
    ...
    
    $app -> get("/home", function ($req)
    {
        global $var;
        require 'views/home.php';
    });
    
    ...
    

    // home.php
    
    var_dump($var);
    var_dump($req);
    

    better is OOP approach and passing data through parameters

    edit: if you ask just how to pass variable across files then the above is fine, thing is when you look at code of view file, after some time, you will not know from where the variable is comming and that force you to read more source files (IDE won't tell you where the variable is defined).

    consider this:

    $app -> get("/home", function ($req)
    {
        global $var;
        require 'views/home.php';
        $view = new Home($var);
    });
    
    class Home {
        private $var;
        public function __construct($var){
            $this->var = $var;
        }
        public function render(){
            echo $this->var;
        }
    }
    

    from this perspective you see instantly that there is variable var and that it comes from constructor, no confusion, only way how variable could be passed in is object creation which is much easier to find and IDE will tell you where