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.