After getting the requested controller from the URI request, it checks whether this exists and whether the requested method (default index) exists within that controller, then calls them.
My intention is to have everything running object oriented so I'm trying to remove any procedural/unattached variables that may appear throughout the code.
My question is, can this code be refined to be more efficient? It works fine, however I've noticed a small increment in time between requesting controllers/methods and I'm not sure where this slow behaviour is coming from.
Note: the intention of this code is to check the URI string (I don't use .htaccess for this) and request a controller and a method from the URI string, for example: site.com/controller/method, site.com/login/recover
index.php
namespace MVC;
use MVC\libs\URI_Request;
class Index {
private $controller;
private $method;
function __construct() {
$this->autoload();
$__REQUEST__ = new URI_Request($_SERVER["REQUEST_URI"]);
$this->prepareController($__REQUEST__);
$this->method = $__REQUEST__->getMethod();
if($this->checkClass()) {
$this->controller = new $this->controller();
if($this->checkMethod($__REQUEST__)) {
$this->controller->{$this->method}();
}
}
}
function autoload() {
require_once("Autoloader.php");
new Autoloader();
}
function checkClass() {
if(class_exists($this->controller)) {
return true;
} else {
echo "controller: error (" . $this->controller . ") does not exist<br>";
}
return false;
}
function checkMethod($request) {
if(method_exists($this->controller, $this->method)) {
return true;
} else {
echo "method: (" . $this->method . ") does not exist for requested controller.<br>";
}
}
function prepareController($__REQUEST__) {
$this->controller = ucfirst($__REQUEST__->getController()) . "Controller";
$this->controller = '\MVC\controllers\\' . $this->controller;
}
}
new Index();
routing.php (prepended file to any request)
<?php
if (preg_match('/\.(?:png|jpg|jpeg|gif)$/', $_SERVER["REQUEST_URI"])) {
return false;
} else {
include 'index.php';
}
URI_Request.php
<?php
namespace MVC\libs;
class URI_Request {
private $request;
private $pairs;
private $controller;
private $method;
function __construct($request) {
$this->request = trim($request);
$this->request = explode("/", $this->request);
$this->request = array_filter($this->request);
$this->request = $this->handle($request);
}
function handle($request) {
foreach($this->request as $key=>$item) {
switch($key) {
case 1:
$this->controller = $item;
break;
case 2:
$this->method = $item;
break;
default:
if($key > 2) {
$item = explode("=", $item);
if(isset($item[1])) {
//key item pair, data
$this->pairs[$item[0]] = $item[1];
} else if(isset($item[0])) {
echo "<b>WARNING: \"" . $item[0] . "\" has no key/value!</b>";
}
}
break;
}
}
if(!$this->controller){
$this->controller = 'index';
}
if(!$this->method) {
$this->method = 'index';
}
if(!$this->pairs) {
$this->pairs = [];
}
}
function getController() {
return $this->controller;
}
function getMethod() {
return $this->method;
}
function getPairs() {
return $this->pairs;
}
function getRequest() {
return $this->request;
}
}
Autoloader.php
<?php
namespace MVC;
class Autoloader {
function __construct() {
session_start();
spl_autoload_register('self::loader');
}
static public function loader($className) {
$filename = str_replace('\\', '/', $className) . '.php';
$filename = str_replace('MVC/', '', $filename);
if(file_exists($filename)) {
require($filename);
}
}
}