-
-
Save anonymous/2cec8213ef06f06ac6e3 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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; | |
} | |
} | |
?> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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'); | |
} | |
} | |
?> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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