As Ravensholde approaches Public Beta, I wanted to refactor the games structure to match more like what a professional app would be like.
The file structure is as follows:
Classes
Blocks
Block
Controllers
Controller
Database
Connection
Helpers
Helper
Models
Model
Templates
Admin
Base
Game
With composer.json setting the autoload PSR for the Classes.
Index.php has been re-written to work as a router, like so (pseudo code to illustrate):
<?php
require 'vendor/autoload.php';
use Classes\Blocks\Block;
use Classes\Controllers\Controller;
use Classes\Database\Connection;
use Classes\Helpers\Helper;
use Classes\Models\Model;
class Index {
protected Model $model;
protected Helper $helper;
public function __construct(
Model $model;
Helper $helper;
) {
$this->model = $model;
$this->helper = $helper;
}
public function execute()
{
session_start();
// A router for a template
$this->helper->addRoute('method', 'url', function(){
$model = $this->model;
$helper = $this->helper;
$block = new Block($model, $helper);
require_once('templates/base/template.php');
});
// A router for a controller
$this->helper->addRoute('method', 'url', function(){
$model = $this->model;
$helper = $this->helper;
$controller = new Controller($model, $helper);
echo $controller-execute();
});
}
}
// Only create one connection to be used throughout
$mysqliAdapter = new Connection();
$mysqliConnection = $mysqliAdapter::connection();
// Initialise models and helpers
$model = new Model();
$helper = new Helper($model);
// Initialise the router
$index = new Index(
$model,
$helper
);
// execute the router
$index->execute();
This has made the code base much cleaner, and will be far easier to maintain long term.