From a739a66ae426af92205c379ac82579c26785bbb8 Mon Sep 17 00:00:00 2001 From: Cerbu Andrei Mihnea <100320074+andrei-mihnea-cerbu@users.noreply.github.com> Date: Wed, 10 May 2023 03:28:30 +0300 Subject: [PATCH] gata basic ul --- knowyourfood/controllers/AuthController.php | 1 - knowyourfood/controllers/siteController.php | 45 ++++++++++---- knowyourfood/core/Application.php | 6 +- knowyourfood/core/Controller.php | 2 +- knowyourfood/core/Router.php | 66 +++------------------ knowyourfood/core/Session.php | 10 ++-- knowyourfood/core/View.php | 45 ++++++++++++++ knowyourfood/core/form/BaseField.php | 35 +++++++++++ knowyourfood/core/form/Form.php | 4 +- knowyourfood/core/form/InputField.php | 36 +++++++++++ knowyourfood/core/form/TextareaField.php | 13 ++++ knowyourfood/index.php | 4 +- knowyourfood/models/ContactForm.php | 28 +++++++++ knowyourfood/views/contact.php | 27 ++++----- knowyourfood/views/home.php | 4 ++ knowyourfood/views/layouts/main.php | 12 +++- knowyourfood/views/login.php | 9 ++- knowyourfood/views/profile.php | 6 ++ knowyourfood/views/register.php | 12 ++-- 19 files changed, 255 insertions(+), 110 deletions(-) create mode 100644 knowyourfood/core/View.php create mode 100644 knowyourfood/core/form/BaseField.php create mode 100644 knowyourfood/core/form/InputField.php create mode 100644 knowyourfood/core/form/TextareaField.php create mode 100644 knowyourfood/models/ContactForm.php diff --git a/knowyourfood/controllers/AuthController.php b/knowyourfood/controllers/AuthController.php index 0277e13..052974c 100644 --- a/knowyourfood/controllers/AuthController.php +++ b/knowyourfood/controllers/AuthController.php @@ -43,7 +43,6 @@ class AuthController extends Controller{ $user->loadData($request->getBody()); if($user->validate() && $user->save()){ - Application::$app->session->setFlash('success', 'Thanks for registering'); $response->redirect('home'); exit; } diff --git a/knowyourfood/controllers/siteController.php b/knowyourfood/controllers/siteController.php index 39c8554..bc8dc36 100644 --- a/knowyourfood/controllers/siteController.php +++ b/knowyourfood/controllers/siteController.php @@ -1,20 +1,41 @@ getBody(); - return 'handling requested data'; +class SiteController extends Controller +{ + public function contact(Request $request, Response $response) + { + $contact = new ContactForm(); + + if ($request->isPost()) { + $contact->loadData($request->getBody()); + + if ($contact->validate() && $contact->send()) { + Application::$app->session->setFlash('success', 'The form has been succesfully sent!'); + var_dump($_SESSION['flash_messages']); + $response->redirect('contact'); + exit; + } + + return $this->render('contact', [ + 'model' => $contact + ]); + } + + return $this->render('contact', [ + 'model' => $contact + ]); } - public function contact(){ - return $this->render('contact'); - } - - public function home(){ + public function home() + { $params = [ 'name' => "TheCodeholic" ]; @@ -22,11 +43,13 @@ class SiteController extends Controller{ return $this->render('home', $params); } - public function profile(){ + public function profile() + { return $this->render('profile'); } - public function _404(){ + public function _404() + { return $this->render('_404'); } } \ No newline at end of file diff --git a/knowyourfood/core/Application.php b/knowyourfood/core/Application.php index 7206480..9f414fd 100644 --- a/knowyourfood/core/Application.php +++ b/knowyourfood/core/Application.php @@ -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 ]); } diff --git a/knowyourfood/core/Controller.php b/knowyourfood/core/Controller.php index fcbb677..39577ca 100644 --- a/knowyourfood/core/Controller.php +++ b/knowyourfood/core/Controller.php @@ -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){ diff --git a/knowyourfood/core/Router.php b/knowyourfood/core/Router.php index cb7cadf..403471e 100644 --- a/knowyourfood/core/Router.php +++ b/knowyourfood/core/Router.php @@ -1,6 +1,7 @@ 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(); - } } \ No newline at end of file diff --git a/knowyourfood/core/Session.php b/knowyourfood/core/Session.php index a4cf901..84c7679 100644 --- a/knowyourfood/core/Session.php +++ b/knowyourfood/core/Session.php @@ -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]); } } diff --git a/knowyourfood/core/View.php b/knowyourfood/core/View.php new file mode 100644 index 0000000..e4583d1 --- /dev/null +++ b/knowyourfood/core/View.php @@ -0,0 +1,45 @@ +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(); + } +} \ No newline at end of file diff --git a/knowyourfood/core/form/BaseField.php b/knowyourfood/core/form/BaseField.php new file mode 100644 index 0000000..8f544c7 --- /dev/null +++ b/knowyourfood/core/form/BaseField.php @@ -0,0 +1,35 @@ +model = $model; + $this->attribute = $attribute; + } + + abstract public function renderInput(): string; + + public function __toString() + { + return sprintf(' +