Initial commit

This commit is contained in:
Porawit Dongwang
2026-09-16 23:20:08 +07:00
commit 0041668dbb
32577 changed files with 3687927 additions and 0 deletions
@@ -0,0 +1,43 @@
<?php
namespace app\Core;
class App {
protected $controller = 'Home';
protected $method = 'index';
protected $params = [];
public function __construct() {
$url = $this->parseUrl();
// Check if controller exists
if(isset($url[0]) && file_exists('../app/Controllers/' . ucfirst($url[0]) . 'Controller.php')) {
$this->controller = ucfirst($url[0]);
unset($url[0]);
}
require_once '../app/Controllers/' . $this->controller . 'Controller.php';
$controllerClass = '\\app\\Controllers\\' . $this->controller . 'Controller';
$this->controller = new $controllerClass;
// Check if method exists
if(isset($url[1])) {
if(method_exists($this->controller, $url[1])) {
$this->method = $url[1];
unset($url[1]);
}
}
// Get params
$this->params = $url ? array_values($url) : [];
// Call the method with params
call_user_func_array([$this->controller, $this->method], $this->params);
}
public function parseUrl() {
if(isset($_GET['url'])) {
return explode('/', filter_var(rtrim($_GET['url'], '/'), FILTER_SANITIZE_URL));
}
return [];
}
}
@@ -0,0 +1,22 @@
<?php
namespace app\Core;
class Controller {
// Load model
public function model($model) {
require_once '../app/Models/' . $model . '.php';
$modelClass = '\\app\\Models\\' . $model;
return new $modelClass();
}
// Load view
public function view($view, $data = []) {
if(file_exists('../app/Views/' . $view . '.php')) {
// Extract data to variables for the view
extract($data);
require_once '../app/Views/' . $view . '.php';
} else {
die('View does not exist');
}
}
}
@@ -0,0 +1,89 @@
<?php
namespace app\Core;
use PDO;
use PDOException;
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 . ';charset=utf8mb4';
$options = array(
PDO::ATTR_PERSISTENT => true,
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_OBJ
);
// Create PDO instance
try {
$this->dbh = new PDO($dsn, $this->user, $this->pass, $options);
} catch(PDOException $e) {
$this->error = $e->getMessage();
// Fallback for when DB isn't created yet during dev
// echo $this->error;
}
}
// Prepare statement
public function query($sql) {
if ($this->dbh) {
$this->stmt = $this->dbh->prepare($sql);
}
}
// Bind values
public function bind($param, $value, $type = null) {
if(is_null($type)) {
switch(true) {
case is_int($value):
$type = PDO::PARAM_INT;
break;
case is_bool($value):
$type = PDO::PARAM_BOOL;
break;
case is_null($value):
$type = PDO::PARAM_NULL;
break;
default:
$type = PDO::PARAM_STR;
}
}
$this->stmt->bindValue($param, $value, $type);
}
// Execute the prepared statement
public function execute() {
return $this->stmt->execute();
}
// Get multiple records as array of objects
public function resultSet() {
$this->execute();
return $this->stmt->fetchAll();
}
// Get single record as object
public function single() {
$this->execute();
return $this->stmt->fetch();
}
// Get row count
public function rowCount() {
return $this->stmt->rowCount();
}
// Get last insert ID
public function lastInsertId() {
return $this->dbh->lastInsertId();
}
}