Skip to content

Instantly share code, notes, and snippets.

/BDClient.php Secret

Created August 20, 2013 16:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anonymous/2cec8213ef06f06ac6e3 to your computer and use it in GitHub Desktop.
Save anonymous/2cec8213ef06f06ac6e3 to your computer and use it in GitHub Desktop.
<?php
class DBConnect
{
const DB_NAME = "test.db";
private $db;
static private $_instance = null;
private function __construct()
{
$db = new PDO("mysql:host=localhost;dbname=test.db", "root", "");
}
static function getInstance()
{
if (!self::$_instance) {
self::$_instance = new DBConnect();
}
return self::$_instance;
}
}
?>
<?php
class MainController extends Controller
{
function __construct()
{
$this->view = new View();
$this->model = new MainModel();
}
function actionIndex()
{
$data = $this->model->get_data();
$this->view->generate('mainView.php', $data);
}
}
<?php
class MainModel extends Model
{
public function get_data()
{
$db = DBConnect::getInstance();
$msq= "SELECT * FROM post ORDER by id DESC";
var_dump($db);
$result = $db->query($msq);
return $result;
}
}
<?php
class Router
{
public function start()
{
$controllerName = "Main";
$actionName = "Index";
$routes = explode("/", $_SERVER['REQUEST_URI']);
if (!empty($routes[2])) {
$controllerName = $routes[2];
}
if (!empty($route[3])) {
$actionName = $route[3];
}
$modelName = $controllerName . "Model";
$controllerName = $controllerName . "Controller";
$actionName = "action" . $actionName;
$modelFile = $modelName . ".php";
$modelPath = "application/models/" . $modelFile;
if (file_exists($modelPath)) {
include $modelPath;
}
$controllerFile = $controllerName . ".php";
$controllerPath = "application/controllers/" . $controllerFile;
if (file_exists($controllerPath)) {
include $controllerPath;
} else {
$this->error404();
}
$controller = new $controllerName;
$action = $actionName;
if (method_exists($controller, $action)) {
$controller->$action();
} else {
$this->error404();
}
}
private function error404()
{
$host = 'http://' . $_SERVER['HTTP_HOST'] . '/' . "MVC/";
header('HTTP/1.1 404 Not Found');
header("Status: 404 Not Found");
header('Location:' . $host . 'Unknow');
}
}
?>
<?php
class View
{
function generate($contentView, $date = null)
{
if (is_array($date)) {
extract($date);
}
include "application/views/templateView.php";
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment