microserviciile de register, login finalizate
This commit is contained in:
Regular → Executable
+87
-69
@@ -1,70 +1,88 @@
|
||||
<?php
|
||||
|
||||
namespace app\controllers;
|
||||
use app\core\middlewares\AuthMiddleware;
|
||||
use app\core\Application;
|
||||
use app\core\Controller;
|
||||
use app\core\Request;
|
||||
use app\core\Response;
|
||||
use app\models\LoginForm;
|
||||
use app\models\User;
|
||||
|
||||
class AuthController extends Controller{
|
||||
public function __construct(){
|
||||
$this->registerMiddleware(new AuthMiddleware(['profile']));
|
||||
}
|
||||
|
||||
public function login(Request $request, Response $response){
|
||||
$loginForm = new LoginForm();
|
||||
|
||||
if($request->isPost()){
|
||||
$loginForm->loadData($request->getBody());
|
||||
if($loginForm->validate() && $loginForm->login()){
|
||||
$response->redirect('home');
|
||||
exit;
|
||||
}
|
||||
|
||||
$this->setLayout('auth');
|
||||
return $this->render('login', [
|
||||
'model' => $loginForm
|
||||
]);
|
||||
}
|
||||
|
||||
$this->setLayout('auth');
|
||||
return $this->render('login', [
|
||||
'model' => $loginForm
|
||||
]);
|
||||
}
|
||||
|
||||
public function register(Request $request, $response){
|
||||
$user = new User();
|
||||
|
||||
if($request->isPost()){
|
||||
$user->loadData($request->getBody());
|
||||
|
||||
if($user->validate() && $user->save()){
|
||||
$response->redirect('home');
|
||||
exit;
|
||||
}
|
||||
|
||||
$this->setLayout('auth');
|
||||
return $this->render('register', [
|
||||
'model'=>$user
|
||||
]);
|
||||
}
|
||||
|
||||
$this->setLayout('auth');
|
||||
return $this->render('register', [
|
||||
'model'=>$user
|
||||
]);
|
||||
}
|
||||
|
||||
public function logout($request, $response){
|
||||
Application::$app->logout();
|
||||
$response->redirect('/');
|
||||
}
|
||||
|
||||
public function profile(){
|
||||
return $this->render('profile');
|
||||
}
|
||||
<?php
|
||||
|
||||
namespace app\controllers;
|
||||
use app\core\Application;
|
||||
use app\core\Controller;
|
||||
use app\core\Request;
|
||||
use app\models\LoginModel;
|
||||
use app\core\Model;
|
||||
use app\models\RegisterModel;
|
||||
|
||||
class AuthController extends Controller{
|
||||
public function __construct(){
|
||||
}
|
||||
|
||||
public static function loginPage(Request $request): string
|
||||
{
|
||||
$model = new LoginModel();
|
||||
|
||||
if($request->isPost()){
|
||||
$model->loadData($request->getPOSTBody());
|
||||
$model->checkForErrors();
|
||||
|
||||
if($model->hasErrors()){
|
||||
self::setControllerParams("Login", "Login", "Auth", $model);
|
||||
return Application::$app->controller->render();
|
||||
}
|
||||
$model->removeErrors();
|
||||
|
||||
$body = $model->createBody();
|
||||
$headers = $model->createHeaders(json_encode($body));
|
||||
|
||||
$model->setParametersForRequest("GET", $headers, $body);
|
||||
|
||||
$model->execute();
|
||||
if($model->isRequestFulfilled()){
|
||||
//Application::$app->response->redirect("homepage");
|
||||
}
|
||||
|
||||
$model->setRequestError();
|
||||
self::setControllerParams("Login", "Login", "Auth", $model);
|
||||
return Application::$app->controller->render();
|
||||
}
|
||||
|
||||
self::setControllerParams("Login", "Login", "Auth", $model);
|
||||
return Application::$app->controller->render();
|
||||
}
|
||||
|
||||
public static function registerPage(Request $request): string
|
||||
{
|
||||
$model = new RegisterModel();
|
||||
|
||||
if($request->isPost()){
|
||||
$model->loadData($request->getPOSTBody());
|
||||
$model->checkForErrors();
|
||||
|
||||
if($model->hasErrors()){
|
||||
self::setControllerParams("Register", "Register", "Auth", $model);
|
||||
return Application::$app->controller->render();
|
||||
}
|
||||
$model->removeErrors();
|
||||
|
||||
$body = $model->createBody();
|
||||
$headers = $model->createHeaders(json_encode($body));
|
||||
|
||||
$model->setParametersForRequest("PUT", $headers, $body);
|
||||
|
||||
$model->execute();
|
||||
if($model->isRequestFulfilled()){
|
||||
Application::$app->response->redirect("login");
|
||||
}
|
||||
|
||||
$model->setRequestError();
|
||||
self::setControllerParams("Register", "Register", "Auth", $model);
|
||||
return Application::$app->controller->render();
|
||||
}
|
||||
|
||||
self::setControllerParams("Register", "Register", "Auth", $model);
|
||||
return Application::$app->controller->render();
|
||||
}
|
||||
|
||||
public static function setControllerParams(string $title, string $view, string $layout, ?Model $model): void
|
||||
{
|
||||
Application::$app->controller->setLayout($layout);
|
||||
Application::$app->controller->setModel($model);
|
||||
Application::$app->controller->setTitle($title);
|
||||
Application::$app->controller->setView($view);
|
||||
}
|
||||
}
|
||||
Executable
+47
@@ -0,0 +1,47 @@
|
||||
<?php
|
||||
|
||||
namespace app\controllers;
|
||||
|
||||
use app\core\Application;
|
||||
use app\core\Controller;
|
||||
use app\core\Model;
|
||||
use app\core\Request;
|
||||
use app\core\Response;
|
||||
use app\models\ContactForm;
|
||||
|
||||
class SiteController extends Controller
|
||||
{
|
||||
|
||||
|
||||
public function Homepage(): string
|
||||
{
|
||||
|
||||
return $this->render();
|
||||
}
|
||||
|
||||
public function profile(): string
|
||||
{
|
||||
return $this->render('profile');
|
||||
}
|
||||
|
||||
public static function renderMainPage(): string
|
||||
{
|
||||
Application::$app->view->setLayout('Main');
|
||||
return Application::$app->view->renderLayout();
|
||||
}
|
||||
|
||||
public function _404(): string
|
||||
{
|
||||
return $this->render();
|
||||
}
|
||||
|
||||
public function _403(): string
|
||||
{
|
||||
return $this->render();
|
||||
}
|
||||
|
||||
protected static function setControllerParams(string $title, string $view, string $layout, ?Model $model)
|
||||
{
|
||||
// TODO: Implement setControllerParams() method.
|
||||
}
|
||||
}
|
||||
@@ -1,55 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace app\controllers;
|
||||
|
||||
use app\core\Application;
|
||||
use app\core\Controller;
|
||||
use app\core\Request;
|
||||
use app\core\Response;
|
||||
use app\models\ContactForm;
|
||||
|
||||
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 home()
|
||||
{
|
||||
$params = [
|
||||
'name' => "TheCodeholic"
|
||||
];
|
||||
|
||||
return $this->render('home', $params);
|
||||
}
|
||||
|
||||
public function profile()
|
||||
{
|
||||
return $this->render('profile');
|
||||
}
|
||||
|
||||
public function _404()
|
||||
{
|
||||
return $this->render('_404');
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user