Making a routing system

Hi,
I wrote this code for routing:

<?php
class APP 
{
    protected $controller = "home";
    protected $method = "index";
    protected $params = [];
    public function Run()
    {
        $url = $this->SplitURL();
        if(file_exists("../app/controller/".strtolower($url[0]).".php"))
        {
            $this->controller = strtolower($url[0]);
            unset($url[0]);
        }
        require "../app/controller/".$this->controller.".php";

        if(isset($url[1]))
        {
            if(method_exists($this->controller, $url[1]))
            {
                $this->method = $url[1];
                unset($url[1]);
            }
        }
        //array_values function resets array's empty indexes
        $this->params = array_values($url);
        //This function calls methods in controller

        call_user_func_array([$this->controller, $this->method], $this->params);

    }

    private function SplitURL()
    {
        if($_GET == null)
        {
            return array("home/index");
        }
        else
        {
            return explode("/", filter_var(trim($_GET['url'], '/')),FILTER_SANITIZE_URL);
        }
    }
}

The home.php file:

<?php
class Home
{
    public function index()
    {
        $this->View("gallery/index");
    }
    public function View($view, $data = '')
    {
        if(file_exists("../app/views/".$view.".php"))
        {
            include "../app/views/".$view.".php";
        }
    }
}
?>

But it shows error when I want to call a function in home class:

Fatal error: Uncaught TypeError: call_user_func_array(): Argument #1 ($function) must be a valid callback, non-static method Home::index() cannot be called statically

Please help me.

You need to look at this function and see what inputs it needs. Then, match them up with the three that you are sending to it here to see if they are the correct ones. You did not show us the “call_user_func_array()” function.

The function is in the home.php file. I have provided its code above (The second block of code).
It searches for the “home class” and “index” method.

When I use $this->controller = new $this->controller above the call_user_func_array([$this->controller, $this->method], $this->params); it works. When I enter http://localhost/myproject/public/home it loads the page correctly but when I enter http://localhost/myproject/public/home/index it just loads html without any CSS. Why??
How can I fix it?

Sponsor our Newsletter | Privacy Policy | Terms of Service