The way the TT code is set up is old fashioned so heres a mvc core i added to my pdo project
im in the process of making my way through all the files and quickly moving to controllers which will just leave separating the ui
thought id share if anyone wants to see how i do it, create 4 folders
core, controllers, views & models
.htaccess (route everything through index)
init.php (this is functions in v3 the defines are different as well BASEPATH/BACKEND)
core/Controller (loads models/views)
core/Database.php (2nd dbclass which will replce the static one)
core/Router.php (use url to get contoller/method/parameter) clean urls
index.php (notice we keep stdhead, dbconn & stdfoot) these 3 functions need replacing but for now we keep them as they are
for how i started using it
this is my staff controller which is the old staff.php code placed in a class that extends the core/ controller
we then add model to constructor im still in process of updating the root files so ill post some more better examples with the ui & model separated
still need to finish models as well, thats why we keep old db so everything works as it should till we get to it
instead of staff.php in url use staff
first we remove these from staff.php
Then we make it a class controller
controllers/Staff.php (Naming is important for autoload eg Torrentsupload.php)
im in the process of making my way through all the files and quickly moving to controllers which will just leave separating the ui
thought id share if anyone wants to see how i do it, create 4 folders
core, controllers, views & models
.htaccess (route everything through index)
<IfModule mod_rewrite.c>
Options -Multiviews
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.+)$ index.php?url=$1 [QSA,L]
</IfModule> init.php (this is functions in v3 the defines are different as well BASEPATH/BACKEND)
require_once (TTROOT."/core/Database.php");
require_once (TTROOT."/core/Controller.php");
require_once (TTROOT."/core/Router.php");core/Controller (loads models/views)
<?php
/*
* Base Controller
* Loads the models and views
*/
class Controller {
// Load model
public function model($model){
// Require model file
require_once TTROOT.'/models/' . $model . '.php';
// Instatiate model
return new $model();
}
// Load view
public function view($view, $data = []){
// Check for view file
if(file_exists(TTROOT.'/views/' . $view . '.php')){
require_once TTROOT.'/views/' . $view . '.php';
} else {
// View does not exist
die('View does not exist');
}
}
} core/Database.php (2nd dbclass which will replce the static one)
<?php
/*
* PDO Database Class
* Connect to database
* Create prepared statements
* Bind values
* Return rows and results
*/
class Database {
private $host = DB_HOST;
private $user = DB_USER;
private $pass = DB_PASS;
private $dbname = DB_NAME;
private $dbh;
private $stmt;
private $error;
public function __construct(){
// Set DSN
$dsn = 'mysql:host=' . $this->host . ';dbname=' . $this->dbname;
$options = array(
PDO::ATTR_PERSISTENT => true,
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION
);
// Create PDO instance
try{
$this->dbh = new PDO($dsn, $this->user, $this->pass, $options);
} catch(PDOException $e){
$this->error = $e->getMessage();
echo $this->error;
}
}
// Chained Prepared Statements
public function run($sql, $bind = NULL){
if (!$bind)
{
return $this->dbh->query($sql);
}
$stmt = $this->dbh->prepare($sql);
$stmt->execute($bind);
return $stmt;
}
} core/Router.php (use url to get contoller/method/parameter) clean urls
<?php
/*
* App Router Class
* Creates URL & loads core controller
* URL FORMAT - /controller/method/params
*/
class Router {
protected $currentController = 'index';
protected $currentMethod = 'index';
protected $params = [];
public function __construct(){
//print_r($this->getUrl());
$url = $this->getUrl();
// Look in controllers for first value
if(file_exists(TTROOT.'/controllers/' . ucwords($url[0]). '.php')){
// If exists, set as controller
$this->currentController = ucwords($url[0]);
// Unset 0 Index
unset($url[0]);
}
// Require the controller
require_once TTROOT.'/controllers/'. $this->currentController . '.php';
// Instantiate controller class
$this->currentController = new $this->currentController;
// Check for second part of url
if(isset($url[1])){
// Check to see if method exists in controller
if(method_exists($this->currentController, $url[1])){
$this->currentMethod = $url[1];
// Unset 1 index
unset($url[1]);
}
}
// Get params
$this->params = $url ? array_values($url) : [];
// Call a callback with array of params
call_user_func_array([$this->currentController, $this->currentMethod], $this->params);
}
public function getUrl(){
if(isset($_GET['url'])){
$url = rtrim($_GET['url'], '/');
$url = filter_var($url, FILTER_SANITIZE_URL);
$url = explode('/', $url);
return $url;
}
}
}
index.php (notice we keep stdhead, dbconn & stdfoot) these 3 functions need replacing but for now we keep them as they are
<?php
require_once("backend/init.php");
dbconn(true);
loggedinonly ();
stdhead(T_("HOME"));
// Init Router Library
$init = new Router;
stdfoot(); for how i started using it
this is my staff controller which is the old staff.php code placed in a class that extends the core/ controller
we then add model to constructor im still in process of updating the root files so ill post some more better examples with the ui & model separated
still need to finish models as well, thats why we keep old db so everything works as it should till we get to it
instead of staff.php in url use staff
first we remove these from staff.php
require_once("backend/init.php");
dbconn();
stdhead();
stdfoot();Then we make it a class controller
controllers/Staff.php (Naming is important for autoload eg Torrentsupload.php)
<?php
class Staff extends Controller {
public function __construct(){
$this->userModel = $this->model('User');
}
public function index(){
$dt = get_date_time(gmtime() - 180);
$res = DB::run("SELECT `users`.`id`, `users`.`username`, `users`.`class`, `users`.`last_access` FROM `users` INNER JOIN `groups` ON `users`.`class` = `groups`.`group_id` WHERE `users`.`enabled` =? AND `users`.`status` =? AND `groups`.`staff_page` =? ORDER BY `username`", ['yes', 'confirmed', 'yes']);
$col = []; //undefined var
$table = []; //undefined var
while ( $row = $res->fetch(PDO::FETCH_ASSOC) )
{
$table[$row["class"]] = ($table[$row["class"]]).
"<td><img src='/images/button_o".($row["last_access"] > $dt ? "n" : "ff")."line.png' alt='' /> ".
"<a href='account-details.php?id=".$row["id"]."'>" . (new Curuser)->class_colour($row["username"]) . "</a> ".
"<a href='mailbox.php?compose&id=".$row["id"]."'><img src='/images/button_pm.gif' border='0' alt='' /></a></td>";
$col[$row['class']] = ($col[$row['class']] ?? 0) + 1;
if ($col[$row["class"]] <= 4)
$table[$row["class"]] = $table[$row["class"]] . "<td></td>";
else
{
$table[$row["class"]] = $table[$row["class"]] . "</tr><tr>";
$col[$row["class"]] = 2;
}
}
$where = null;
if ($CURUSER["edit_users"] == "no")
$where = "AND `staff_public` = 'yes'";
$res = DB::run("SELECT `group_id`, `level`, `staff_public` FROM `groups` WHERE `staff_page` = 'yes' $where ORDER BY `staff_sort`");
if ($res->rowCount() == 0)
show_error_msg(T_("ERROR"), T_("NO_STAFF_HERE"), 1);
begin_frame(T_("STAFF"));
?>
<div class="table-responsive"><table class="table">
<?php while ($row = $res->fetch(PDO::FETCH_ASSOC)): if ( !isset($table[$row["group_id"]]) ) continue; ?>
<tr>
<td colspan="14" class="newtd"><center><b><?php echo T_($row["level"]); ?></b> <?php if ($row["staff_public"] == "no") echo("<font color='#ff0000'>[".T_("HIDDEN FROM PUBLIC")."]</font>"); ?><center></td>
</tr>
<tr>
<?php echo $table[$row["group_id"]]; ?>
</tr>
<?php endwhile; ?>
</table></div>
<?php
end_frame();
}
}
