ladogton2010's picture
cleaning controllers
789f143
<?php
/*
Map URL from browser as the following syntax
fqdn[/path]/controller/view/[params/]
example:
example.com/welcome/index
example.com/welcome/test/4
*/
class Core{
protected $defaultController;
protected $defaultMethod;
protected $defaultParams;
public function __construct(){
$this->defaultController = ucwords('welcome');
$this->defaultMethod = 'index';
$this->defaultParams = [];
// print_r($this->getURL());
$url = $this->getURL();
if(isset($url[0])){
if (file_exists('../app/controllers/' . ucwords($url[0]) . '.php')) {
$this->defaultController = ucwords($url[0]);
unset($url[0]);
}
}
require_once('../app/controllers/' . $this->defaultController . '.php');
$this->defaultController = new $this->defaultController;
if (isset($url[1])) {
if (method_exists($this->defaultController, $url[1])) {
$this->defaultMethod = $url[1];
unset($url[1]);
}
}
$this->defaultParams = $url ? array_values($url) : [];
call_user_func_array([$this->defaultController,$this->defaultMethod],$this->defaultParams);
}
public function getURL(){
// echo $_GET['url'];
if (isset($_GET['url'])) {
$url = rtrim($_GET['url'], '/');
$url = filter_var($url,FILTER_SANITIZE_URL);
$url = explode('/', $url);
return $url;
}
}
}