首頁  >  文章  >  web前端  >  表單驗證物件和認證類別的提取

表單驗證物件和認證類別的提取

WBOY
WBOY原創
2024-07-18 12:25:38891瀏覽

Extraction of Form Validation Object and Authenticate Class

在我們之前的專案中,我們學習如何登入或登出註冊用戶。但是今天,我們將學習如何提取表單驗證物件以及如何在專案中提取驗證類別。

在 VS Code 端

要啟動項目,我們需要新增一個名為 Http 的新目錄,然後將控制器移到這個新目錄。接下來,我們需要在Http中新增另一個名為Forms的新目錄,並在該目錄中新增一個新檔案LoginForm。然後,我們需要運行該項目,這會顯示錯誤,因為控制器已移至新目錄,並且需要在routes.php中更新其路由。

$router->get('/', 'index.php');
$router->get('/about', 'about.php');
$router->get('/contact', 'contact.php');
$router->get('/notes', 'notes/index.php')->only('auth');
$router->get('/note', 'notes/show.php');
$router->delete('/note', 'notes/destroy.php');
$router->get('/note/edit', 'notes/edit.php');
$router->patch('/note', 'notes/update.php');
$router->get('/notes/create', 'notes/create.php');
$router->post('/notes', 'notes/store.php');
$router->get('/register', 'registration/create.php')->only('guest');
$router->post('/register', 'registration/store.php')->only('guest');
$router->get('/login', 'session/create.php')->only('guest');
$router->post('/session', 'session/store.php')->only('guest');
$router->delete('/session', 'session/destroy.php')->only('auth');

提取表單驗證對象

要提取表單驗證對象,我們需要前往 session/store.php 並刪除檢查所提供的電子郵件和密碼是否正確的程式碼。然後,我們需要將此程式碼移至 LoginForm.php 文件,該文件位於 Http/Forms 目錄中。

登入表單

LoginForm.php 檔案將包含與使用者登入相關的資料以驗證表單以及指向專案中錯誤的受保護錯誤陣列

<? php
namespace Http\Forms;
use Core\Validator;
class LoginForm {
  protected $errors = [];
  public function validate($email, $password) {
    if (!Validator::email($email)) {
      $this->errors['email'] = 'Please provide a valid email address.';
    }
    if (!Validator::string($password)) {
      $this->errors['password'] = 'Please provide a valid password.';
    }
    return empty($this->errors);
  }
  public function errors() {
    return $this->errors;
  }
  public function error($field, $message) {
    $this->errors[$field] = $message;
  }
}

現在我們可以看到該專案運作良好。

提取驗證類

接下來,為了提取驗證類,我們需要選擇用於驗證使用者身分的所有程式碼段,例如檢查使用者的電子郵件和密碼。然後我們需要新增一個檔案authenticator.php,它將包含一個用於使用者驗證的authenticate類,然後匯入登入和登出功能。

<?ph
namespace Core;
class Authenticator {
  public function attempt($email, $password) {
    $user = App::resolve(Database::class)
      ->query('select * from users where email = :email', [ 'email' => $email ])
      ->find();
    if ($user) {
      if (password_verify($password, $user['password'])) {
        $this->login([ 'email' => $email ]);
        return true;
      }
    }
    return false;
  }
  public function login($user) {
    $_SESSION['user'] = [ 'email' => $user['email'] ];
    session_regenerate_id(true);
  }
  public function logout() {
    $_SESSION = [];
    session_destroy();
    $params = session_get_cookie_params();
    setcookie('PHPSESSID', '', time() - 3600, $params['path'], $params['domain'], $params['secure'], $params['httponly']);
  }
}

更新會話儲存文件

進一步,我們需要返回 session/store.php 並初始化 $form 並用於新使用者登入。然後我們需要實作一個 if 條件來檢查表單是否有效。如果表單無效,我們必須將使用者重新導向到所需的路徑。

<?php
use Core\Authenticator;
use Http\Forms\LoginForm;

$email = $_POST['email'];
$password = $_POST['password'];

$form = new LoginForm();
if ($form->validate($email, $password)) {
  if ((new Authenticator)->attempt($email, $password)) {
    redirect('/');
  }
  $form->error('email', 'No matching account found for that email address and password.');
}
return view('session/create.view.php', [ 'errors' => $form

透過進行這些更改,我們可以提取一個身份驗證類別來更改程式碼的外觀,這意味著易於理解和修改。

希望你已經清楚地理解了

以上是表單驗證物件和認證類別的提取的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn