refactoring MVC
This commit is contained in:
parent
2b57746923
commit
efacc52ddc
@ -1,8 +0,0 @@
|
||||
<?php
|
||||
class Route
|
||||
{
|
||||
public $route;
|
||||
public function __construct() {
|
||||
$this->route = 'admin';
|
||||
}
|
||||
}
|
@ -1,6 +1,18 @@
|
||||
<?php
|
||||
class AboutController {
|
||||
public function index() {
|
||||
print ("AboutController");
|
||||
|
||||
require_once __DIR__ . "/../model/AboutModule.php";
|
||||
require_once __DIR__ . "/../view/View.php";
|
||||
class AboutController
|
||||
{
|
||||
public function actionIndex()
|
||||
{
|
||||
$result = AboutModel::getData();
|
||||
var_dump($result);
|
||||
echo (View::render([
|
||||
'box' => $result,
|
||||
'body' => 'To home',
|
||||
'auth' => false
|
||||
], 'tpl_layout.php'));
|
||||
print("AboutController, А кто то почувствовал перерыв?");
|
||||
}
|
||||
}
|
24
engine/app/model/AboutModule.php
Normal file
24
engine/app/model/AboutModule.php
Normal file
@ -0,0 +1,24 @@
|
||||
<?php
|
||||
|
||||
class AboutModel
|
||||
{
|
||||
static function getData()
|
||||
{
|
||||
$box = [];
|
||||
if (
|
||||
$handle =
|
||||
opendir('./upload')
|
||||
) {
|
||||
while (false !== ($entry = readdir($handle))) {
|
||||
$ext = pathinfo($entry, PATHINFO_EXTENSION);
|
||||
|
||||
if ($ext == 'mp3') {
|
||||
array_push($box, "./upload/$entry");
|
||||
}
|
||||
}
|
||||
|
||||
closedir($handle);
|
||||
}
|
||||
return $box;
|
||||
}
|
||||
}
|
26
engine/app/view/View.php
Normal file
26
engine/app/view/View.php
Normal file
@ -0,0 +1,26 @@
|
||||
<?php
|
||||
|
||||
define('FOLDER_TEMPLATE', __DIR__. '/../template/');
|
||||
|
||||
class View
|
||||
{
|
||||
static function render()
|
||||
{
|
||||
|
||||
// include "model_box.php";
|
||||
|
||||
function render($data, $tpl )
|
||||
{
|
||||
extract($data);
|
||||
// Start output buffering
|
||||
ob_start();
|
||||
// Include the template file
|
||||
include FOLDER_TEMPLATE . $tpl;
|
||||
// Get the contents of the buffer
|
||||
$content = ob_get_contents();
|
||||
// End output buffering and erase the buffer's contents
|
||||
ob_end_clean();
|
||||
return $content;
|
||||
}
|
||||
}
|
||||
}
|
17
engine/core/Autoloader.php
Normal file
17
engine/core/Autoloader.php
Normal file
@ -0,0 +1,17 @@
|
||||
<?php
|
||||
class Autoloader
|
||||
{
|
||||
static function init()
|
||||
{
|
||||
spl_autoload_register(function ($class_name) {
|
||||
// print('auto: ' . $class_name);
|
||||
$dir = ['/../app/controller/'];
|
||||
foreach ($dir as $path) {
|
||||
$path = __DIR__ . $path . $class_name . '.php';
|
||||
if (file_exists($path)) {
|
||||
include_once($path);
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
59
engine/core/Router.php
Normal file
59
engine/core/Router.php
Normal file
@ -0,0 +1,59 @@
|
||||
<?php
|
||||
require_once 'Autoloader.php';
|
||||
Autoloader::init();
|
||||
|
||||
class Router
|
||||
{
|
||||
// массив маршрутов
|
||||
private $routes;
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
// указываем путь к роутам (ROOT - путь к базовой дериктории
|
||||
// '/config/routes.php' - путь к созданному файлу с роутами
|
||||
$routesPath = __DIR__ . '/../routes.php';
|
||||
|
||||
// здесь мы присваиваем свойству $this->routes массив,
|
||||
// который хранится в файле routes.php, при помощи - include
|
||||
$this->routes = include($routesPath);
|
||||
}
|
||||
|
||||
// метод будет принимать управление от фронтконтроллера
|
||||
public function run()
|
||||
{
|
||||
echo ("Запуск контроллера!");
|
||||
// var_dump($this->routes);
|
||||
// обратимся к методу getURI()
|
||||
$uri = $this->getURI();
|
||||
echo $uri;
|
||||
|
||||
foreach ($this->routes as $uriPattern => $path) {
|
||||
// echo "<br>$uriPattern -> $path";
|
||||
if (preg_match("~$uriPattern~", $uri)) {
|
||||
$segments = explode('/', $path);
|
||||
$controllerName = array_shift($segments) . 'Controller';
|
||||
// делает первую букву строки заглавной
|
||||
$controllerName = ucfirst($controllerName);
|
||||
var_dump($controllerName);
|
||||
// выводим имя контроллера
|
||||
$actionName = 'action' . ucfirst(array_shift($segments));
|
||||
$controllerFile = __DIR__ . '/../app/controller/' . $controllerName . '.php';
|
||||
var_dump($controllerFile);
|
||||
if (file_exists($controllerFile)) {
|
||||
include_once($controllerFile);
|
||||
}
|
||||
$controllerObject = new $controllerName;
|
||||
$result = $controllerObject->$actionName();
|
||||
// выводим название экшена
|
||||
echo '<br>' . $result;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private function getURI()
|
||||
{
|
||||
if (!empty($_SERVER['REQUEST_URI'])) {
|
||||
return trim($_SERVER['REQUEST_URI'], '/');
|
||||
}
|
||||
}
|
||||
}
|
@ -1,7 +0,0 @@
|
||||
<?php
|
||||
|
||||
$user = [ 'login' => 'admin', 'pass' => 111];
|
||||
|
||||
extract($user);
|
||||
|
||||
var_dump($login);
|
@ -1,24 +0,0 @@
|
||||
<?php
|
||||
// ini_set('error_reporting', 0);
|
||||
|
||||
function one()
|
||||
{
|
||||
require_once "lib/file2.php";
|
||||
sendMessage();
|
||||
}
|
||||
|
||||
function two()
|
||||
{
|
||||
include_once "lib/file2.php";
|
||||
sendMessage();
|
||||
}
|
||||
|
||||
function three()
|
||||
{
|
||||
include_once "lib/file2.php";
|
||||
sendMessage();
|
||||
}
|
||||
|
||||
one();
|
||||
two();
|
||||
three();
|
@ -1,12 +1,28 @@
|
||||
<?php
|
||||
|
||||
spl_autoload_register(function ($class_name) {
|
||||
$dir = ['/app/controller'];
|
||||
foreach ($dir as $path) {
|
||||
$path = __DIR__ . $path . $class_name . '.php';
|
||||
include $path;
|
||||
}
|
||||
});
|
||||
require_once './core/Autoloader.php';
|
||||
Autoloader::init();
|
||||
|
||||
// Включение отображения ошибок на время разработки сайта
|
||||
ini_set('display_errors', 1);
|
||||
error_reporting(E_ALL);
|
||||
|
||||
// Подключаем файл Router.php используя следующие команды:
|
||||
define('ROOT', dirname(__FILE__));
|
||||
require_once (ROOT . '/core/Router.php');
|
||||
|
||||
//проверка: echo ROOT; (C:\OSPanel\domains\test2)
|
||||
|
||||
// создаем экземпляр класса Router
|
||||
$router = new Router();
|
||||
// и запускаем метод run(), тем самым, передав на него управление
|
||||
$router->run();
|
||||
|
||||
exit;
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
var_dump($_SERVER['REQUEST_URI']);
|
||||
$uri = trim($_SERVER['REQUEST_URI'], '/');
|
||||
@ -51,4 +67,4 @@ class MainView
|
||||
}
|
||||
}
|
||||
|
||||
$cont = new Route('');
|
||||
$cont = new Route();
|
||||
|
9
engine/routes.php
Normal file
9
engine/routes.php
Normal file
@ -0,0 +1,9 @@
|
||||
<?php
|
||||
// маршруты
|
||||
return array(
|
||||
'news' => 'news/index', // actionIndex в NewsController
|
||||
'products' => 'product/list', // actionList в ProductController
|
||||
'about' => 'about/index',
|
||||
);
|
||||
// - где 'news' - строка запроса
|
||||
// 'news/index' - имя контроллера и экшена для обработки этого запроса (путь обработчика)
|
Loading…
Reference in New Issue
Block a user