gata basic ul

This commit is contained in:
Cerbu Andrei Mihnea
2023-05-10 03:28:30 +03:00
parent 90f12ab916
commit a739a66ae4
19 changed files with 255 additions and 110 deletions
+4 -2
View File
@@ -18,6 +18,7 @@ class Application
public Database $db;
public Session $session;
public ?DbModel $user;
public View $view;
public function __construct($rootPath, array $config)
{
@@ -28,6 +29,7 @@ class Application
$this->request = new Request();
$this->response = new Response();
$this->router = new Router($this->request, $this->response);
$this->view = new View();
$this->db = new Database($config['db']);
$this->session = new Session();
@@ -78,12 +80,12 @@ class Application
echo $this->router->resolve();
}catch(ForbiddenException $e){
$this->response->setStatusCode(403);
echo $this->router->renderView('_error', [
echo $this->view->renderView('_error', [
'exception' => $e
]);
}catch(NotFoundException $e){
$this->response->setStatusCode(404);
echo $this->router->renderView('_error', [
echo $this->view->renderView('_error', [
'exception' => $e
]);
}
+1 -1
View File
@@ -9,7 +9,7 @@ class Controller{
protected array $middlewares = [];
public function render($view, $params = []){
return Application::$app->router->renderView($view, $params);
return Application::$app->view->renderView($view, $params);
}
public function setLayout($layout){
+8 -58
View File
@@ -1,6 +1,7 @@
<?php
namespace app\core;
use app\core\exceptions\NotFoundException;
class Router
@@ -25,11 +26,6 @@ class Router
$this->routes['post'][$path] = $callback;
}
/*
in aceasta functie se identifica path ul catre fisier, metoda (GET/POST) si se identifica un "routes" callback ul (in general controller ul) pe care trebuie sa l returneze
astfel, in call_user_function, se ofera clasa de tip constructor si request ul de la user ca dupa controller ul sa se ocupe de restul
*/
public function resolve()
{
$path = $this->request->getPath();
@@ -40,61 +36,15 @@ class Router
throw new NotFoundException();
}
if (is_string($callback)) {
return $this->renderView($callback);
$controller = new $callback[0]();
Application::$app->controller = $controller;
$controller->action = $callback[1];
$callback[0] = $controller;
foreach ($controller->getMiddlewares() as $middleware) {
$middleware->execute();
}
if (is_array($callback)) {
$controller = new $callback[0]();
Application::$app->controller = $controller;
$controller->action = $callback[1];
$callback[0] = $controller;
foreach($controller->getMiddlewares() as $middleware){
$middleware->execute();
}
}
return call_user_func($callback, $this->request, $this->response);
}
//layout ne va ajuta sa identificam ce fel de layout trebuie sa aiba pagina pe care o trimitem (elemente extra in afara de continut: navbar, elemente din baza de date etc)
protected function layoutContent()
{
$layout = Application::$app->layout;
if (Application::$app->controller) {
$layout = Application::$app->controller->layout;
}
ob_start();
include_once Application::$ROOT_DIR . "/views/layouts/$layout.php";
return ob_get_clean();
}
public function renderView($view, $params = [])
{
$layoutContent = $this->layoutContent();
$viewContent = $this->renderOnlyView($view, $params);
return str_replace('{{content}}', $viewContent, $layoutContent);
}
public function renderContent($viewContent)
{
$layoutContent = $this->layoutContent();
return str_replace('{{content}}', $viewContent, $layoutContent);
}
protected function renderOnlyView($view, $params)
{
foreach ($params as $key => $value) {
$$key = $value;
}
ob_start();
include_once Application::$ROOT_DIR . "/views/$view.php";
return ob_get_clean();
}
}
+4 -6
View File
@@ -4,14 +4,12 @@ namespace app\core;
class Session{
protected const FLASH_KEY = 'flash_messages';
public function __construct(){
session_start();
$flashMessages = $_SESSION[self::FLASH_KEY] ?? [];
foreach($flashMessages as $key => &$flashMessage){
foreach($_SESSION[self::FLASH_KEY] as $key => &$flashMessage){
$flashMessage['remove'] = 'true';
}
$_SESSION[self::FLASH_KEY] = $flashMessages;
}
public function setFlash($key, $message){
@@ -30,7 +28,7 @@ class Session{
}
public function get($key){
return $_SESSION[$key] ?? false;
return $_SESSION[$key];
}
public function remove($key){
@@ -40,7 +38,7 @@ class Session{
public function __destruct(){
$flashMessages = $_SESSION[self::FLASH_KEY] ?? [];
foreach($flashMessages as $key => &$flashMessage){
if($flashMessage['remove']){
if($flashMessage['remove'] == 'true'){
unset($flashMessages[$key]);
}
}
+45
View File
@@ -0,0 +1,45 @@
<?php
namespace app\core;
class View{
public string $title = '';
protected function layoutContent()
{
$layout = Application::$app->layout;
if (Application::$app->controller) {
$layout = Application::$app->controller->layout;
}
ob_start();
include_once Application::$ROOT_DIR . "/views/layouts/$layout.php";
return ob_get_clean();
}
public function renderView($view, $params = [])
{
$viewContent = $this->renderOnlyView($view, $params);
$layoutContent = $this->layoutContent();
return str_replace('{{content}}', $viewContent, $layoutContent);
}
public function renderContent($viewContent)
{
$layoutContent = $this->layoutContent();
return str_replace('{{content}}', $viewContent, $layoutContent);
}
protected function renderOnlyView($view, $params)
{
foreach ($params as $key => $value) {
$$key = $value;
}
ob_start();
include_once Application::$ROOT_DIR."/views/$view.php";
return ob_get_clean();
}
}
+35
View File
@@ -0,0 +1,35 @@
<?php
namespace app\core\form;
use app\core\Model;
abstract class BaseField
{
public Model $model;
public string $attribute;
public function __construct(Model $model, string $attribute)
{
$this->model = $model;
$this->attribute = $attribute;
}
abstract public function renderInput(): string;
public function __toString()
{
return sprintf('
<div class="mb-3">
<label>%s</label>
%s
<div class="invalid-feedback">
%s
</div>
</div>
',
$this->model->labels()[$this->attribute] ?? $this->attribute,
$this->renderInput(),
$this->model->getFirstError($this->attribute)
);
}
}
+2 -2
View File
@@ -13,8 +13,8 @@ class Form{
echo '</form>';
}
public function field(Model $model, $attribute){
return new Field($model, $attribute);
public function inputField(Model $model, $attribute){
return new InputField($model, $attribute);
}
public function button(){
+36
View File
@@ -0,0 +1,36 @@
<?php
namespace app\core\form;
use app\core\Model;
class InputField extends BaseField
{
public const TYPE_TEXT = 'text';
public const TYPE_PASSWORD = 'password';
public string $type;
public function __construct(Model $model, string $attribute)
{
$this->type = self::TYPE_TEXT;
parent::__construct($model, $attribute);
}
public function passwordField()
{
$this->type = self::TYPE_PASSWORD;
return $this;
}
public function renderInput(): string
{
return sprintf(
'<input type="%s" name="%s" value="%s" class="form-control%s">',
$this->type,
$this->attribute,
$this->model->{$this->attribute},
$this->model->hasError($this->attribute) ? ' is invalid' : ''
);
}
}
+13
View File
@@ -0,0 +1,13 @@
<?php
namespace app\core\form;
class TextareaField extends BaseField{
public function renderInput(): string{
return sprintf('<textarea name=%s class=form-control%s>%s</textarea>',
$this->attribute,
$this->model->hasError($this->attribute) ? ' is invalid' : '',
$this->model->{$this->attribute}
);
}
}