mvc again

This commit is contained in:
Aleksandr Shelkovin 2025-01-18 13:32:57 +03:00
parent efacc52ddc
commit 2812b5e9d8
8 changed files with 146 additions and 86 deletions

View File

@ -606,6 +606,10 @@ video {
display: none;
}
.h-screen {
height: 100vh;
}
.min-h-screen {
min-height: 100vh;
}
@ -634,6 +638,10 @@ video {
flex-direction: column;
}
.items-center {
align-items: center;
}
.justify-center {
justify-content: center;
}
@ -729,6 +737,11 @@ video {
line-height: 2.25rem;
}
.text-6xl {
font-size: 3.75rem;
line-height: 1;
}
.text-sm {
font-size: 0.875rem;
line-height: 1.25rem;
@ -764,6 +777,11 @@ video {
letter-spacing: 0.025em;
}
.text-blue-500 {
--tw-text-opacity: 1;
color: rgb(59 130 246 / var(--tw-text-opacity, 1));
}
.text-gray-500 {
--tw-text-opacity: 1;
color: rgb(107 114 128 / var(--tw-text-opacity, 1));
@ -799,6 +817,11 @@ video {
color: rgb(126 34 206 / var(--tw-text-opacity, 1));
}
.text-red-500 {
--tw-text-opacity: 1;
color: rgb(239 68 68 / var(--tw-text-opacity, 1));
}
.text-white {
--tw-text-opacity: 1;
color: rgb(255 255 255 / var(--tw-text-opacity, 1));

View File

@ -0,0 +1,15 @@
<?php
require_once __DIR__ . "/../model/AboutModule.php";
require_once __DIR__ . "/../view/View.php";
class MainController
{
public function actionNotFound()
{
// $result = AboutModel::getData();
// var_dump($result);
echo (View::render([], '404.php'));
// print("AboutController, А кто то почувствовал перерыв?");
}
}

View File

@ -0,0 +1,23 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<!-- <script src="https://cdnjs.cloudflare.com/ajax/libs/tailwindcss/4.0.0-beta.9/lib.min.js"></script> -->
<link rel="stylesheet" href="./../../app/assets/css/styles.css">
<title>Page Not Found</title>
</head>
<body>
<div className="flex flex-col items-center justify-center h-screen bg-gray-100">
<h1 className="text-6xl font-bold text-red-500">404</h1>
<p className="mt-4 text-xl">Oops! Page Not Found</p>
<a href="/" className="mt-6 text-blue-500 hover:underline">
Go to Homepage
</a>
</div>
</body>
</html>

View File

@ -2,15 +2,8 @@
define('FOLDER_TEMPLATE', __DIR__ .'/../template/');
class View
{
static function render()
{
// include "model_box.php";
function render($data, $tpl )
{
class View {
static function render( $data, $tpl ) {
extract($data);
// Start output buffering
ob_start();
@ -22,5 +15,6 @@ class View
ob_end_clean();
return $content;
}
}
}

View File

@ -6,6 +6,7 @@ class Autoloader
spl_autoload_register(function ($class_name) {
// print('auto: ' . $class_name);
$dir = ['/../app/controller/'];
// print($dir);
foreach ($dir as $path) {
$path = __DIR__ . $path . $class_name . '.php';
if (file_exists($path)) {

View File

@ -2,51 +2,54 @@
require_once 'Autoloader.php';
Autoloader::init();
require_once __DIR__ . "/../../engine/app/view/View.php";
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;
$controllerName = "";
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();
// var_dump($controllerFile);
}
if (!$controllerName) {
// Page not found
$obj = new MainController();
$obj->actionNotFound();
// var_dump($obj);
// $controllerName = "MainController";
// $actionName = "actionIndex";
// $controllerObject = new $controllerName;
// $result = $controllerObject::$actionName();
break;
}
$controllerObject = new $controllerName;
$result = $controllerObject->$actionName();
$result = $controllerObject::$actionName();
// выводим название экшена
echo '<br>' . $result;
}
}
}

View File

@ -1,14 +1,15 @@
<?php
require_once './core/Autoloader.php';
Autoloader::init();
// require_once './core/Autoloader.php';
// Autoloader::init();
// Включение отображения ошибок на время разработки сайта
ini_set('display_errors', 1);
define('ROOT', dirname(__FILE__));
error_reporting(E_ALL);
// Подключаем файл Router.php используя следующие команды:
define('ROOT', dirname(__FILE__));
require_once (ROOT . '/core/Router.php');
//проверка: echo ROOT; (C:\OSPanel\domains\test2)
@ -24,47 +25,47 @@ exit;
var_dump($_SERVER['REQUEST_URI']);
$uri = trim($_SERVER['REQUEST_URI'], '/');
// var_dump($_SERVER['REQUEST_URI']);
// $uri = trim($_SERVER['REQUEST_URI'], '/');
$mass_uri = explode('/', $uri);
var_dump($mass_uri);
// $mass_uri = explode('/', $uri);
// var_dump($mass_uri);
$controller = $mass_uri[0];
$action = $mass_uri[1] ? $mass_uri[1] : 'index';
$nameClassController = ucfirst($controller) . 'Controller';
// $controller = $mass_uri[0];
// $action = $mass_uri[1] ? $mass_uri[1] : 'index';
// $nameClassController = ucfirst($controller) . 'Controller';
$cntr = new $nameClassController;
$cntr->$action();
// $cntr = new $nameClassController;
// $cntr->$action();
class MainModel
{
public function getData()
{
return [
'title' => 'Main Page',
'h3' => 'С новым годом!'
];
}
}
class MainController
{
public function index()
{
$model = new MainModel();
$view = new MainView();
$result = $model->getData();
$view->render($result, 'main.tpl');
}
}
class MainView
{
public function render($data, $tmpl)
{
var_dump($data);
}
}
// class MainModel
// {
// public function getData()
// {
// return [
// 'title' => 'Main Page',
// 'h3' => 'С новым годом!'
// ];
// }
// }
// class MainController
// {
// public function index()
// {
// $model = new MainModel();
// $view = new MainView();
// $result = $model->getData();
// $view->render($result, 'main.tpl');
// }
// }
// class MainView
// {
// public function render($data, $tmpl)
// {
// var_dump($data);
// }
// }
$cont = new Route();
// $cont = new Route();

View File

@ -1,6 +1,6 @@
/** @type {import('tailwindcss').Config} */
module.exports = {
content: ["./template/*.{html,js}"],
content: ["./app/template/*.{html,js,php}"],
theme: {
extend: {},
},