I tried to make a MVC in PHP with OOP approach. Code Review this so I can understand whether am I using the best practices and can I legally call it a MVC framework or not. Also, I am going with 1 controller for each action.
This blockquote might help you review my code better.
The default thing is actually works like this:
http://localhost/coolcodes/something/test/ (This is bring up the TestController)
http://localhost/coolcodes/something/ (This will bring up the default controller for something).
There is a function called getDefaultRouteName.
getDefaultRouteName => Returns the key for which the default controller is located in the $routesTree variable.
My code:
index.php (The page the client will go to):
<?php
include_once("App.php");
$app = new App();
$app->init();
$app->execute();
?>
App.php (The main application starter):
<?php
include_once("RoutesTree.php");
include_once("Config.php");
class App {
private $controller;
public function init() {
$routesTree = new RoutesTree;
if (Config::isUnderDevelopment()) {
$this->controller = $routesTree->getUnderDevelopmentController();
} else {
$pageURL = str_replace("/coolcodes/api/", "", $_SERVER['REQUEST_URI']);
$path = explode("/", $pageURL);
if ($_SERVER['REQUEST_URI']{strlen($_SERVER['REQUEST_URI']) - 1} == "/") {
$path = array_slice($path, 0, count($path) - 1);
}
$this->controller = $routesTree->getController($path);
}
}
public function execute() {
$this->controller->execute();
}
}
?>
Routes.php (The routes for MVC):
<?php
include_once("Config.php");
class RoutesTree {
private $routesTree;
private $notFoundController;
function __construct() {
$this->initRoutesTree();
}
public function initRoutesTree() {
$this->routesTree = Config::getPreparedRoutesTree();
$this->notFoundController = Config::getNotFoundController();
}
public function getRoutesTree() {
return $this->routesTree;
}
public function getUnderDevelopmentController() {
return Config::getUnderDevelopmentController();
}
public function getController($path) {
$route = $this->routesTree;
foreach ($path as $pathSegments) {
if (array_key_exists($pathSegments, $route)) {
$route = $route[$pathSegments];
} else {
return $this->notFoundController;
}
}
if (is_array($route)) {
return $route[Config::getDefaultRouteName()];
}
return $route;
}
}
?>
Controller.php (MVC Controller base class):
<?php
abstract class Controller {
abstract public function execute();
}
?>
Some Controllers:
TestController.php:
<?php
include_once("Controller.php");
class TestController extends Controller {
public function execute() {
echo json_encode(["value" => "Hello, World!!!!"]);
}
}
?>
SomethingController.php:
<?php
include_once("Controller.php");
class SomethingController extends Controller {
public function execute() {
echo json_encode(["value" => "From something controller."]);
}
}
?>
Controllers that are required:
NotFoundController.php (The Error 404 Controller)
<?php
include_once("Controller.php");
class NotFoundController extends Controller {
public function execute() {
echo "<h1>Sorry, Page not found.</h1>";
}
}
?>
UnderDevelopementController (Controller shown if the website is under construction):
<?php
include_once("Controller.php");
class UnderDevelopmentController extends Controller {
public function execute() {
echo json_encode(["value" => "This website is under construction. See you soon."]);
}
}
?>
Config.php (Settings for MVC (inspired by settings.py in Django))
I made functions instead of variables because I thought that it will be more OOP oriented in PHP (Because I do not want to make variables outside of any classes).
<?php
include_once("allclasses.php");
class Config {
public static function isUnderDevelopment() {
return false;
}
public static function getUnderDevelopmentController() {
return new UnderDevelopmentController;
}
public static function getNotFoundController() {
return new NotFoundController;
}
public static function getDefaultRouteName() {
return "default";
}
public static function getPreparedRoutesTree() {
return [
"something" => [
"default" => new SomethingController,
"test" => new TestController
]
];
}
}
?>
addclasses.php (To Group all the controllers)
<?php
include_once("controllers/TestController.php");
include_once("controllers/SomethingController.php");
include_once("controllers/NotFoundController.php");
include_once("controllers/UnderDevelopmentController.php");
?>
The .htaccess file:
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteRule ^.*$ ./index.php