Skip to content

Instantly share code, notes, and snippets.

@HaradaKumiko
Created January 17, 2022 06:55
Show Gist options
  • Save HaradaKumiko/12ff2967c8f05f8f2563e2a40cb8c3ef to your computer and use it in GitHub Desktop.
Save HaradaKumiko/12ff2967c8f05f8f2563e2a40cb8c3ef to your computer and use it in GitHub Desktop.
<?php
Class Route{
protected $controller = 'HomeController';
protected $method = 'index';
protected $params = [];
public function __construct(){
if(isset($_GET['url'])){
$url = explode('/', filter_var(trim($_GET['url']), FILTER_SANITIZE_URL));
$url[0] = $url[0] . 'Controller';
}else{
$url[0] = 'home';
}
//cek file controller
if( file_exists('../app/Controllers/'. $url[0] .'.php')){
$this->controller = $url[0];
}else{
return require_once '../app/Views/error.php';
}
require_once '../app/Controllers/'. $this->controller .'.php';
$this->controller = new $this->controller;
//cek methode yang ada di controller
if(isset($url[1])){
if(method_exists($this->controller, $url[1])){
$this->method = $url[1];
}
}
unset($url[0]);
unset($url[1]);
$this->params = $url;
call_user_func_array([$this->controller, $this->method], $this->params);
}
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment