問題描述
我不知道哪個是最好的?您認為在控制器中驗證 用戶登錄表單
或其他表單更好還是定義一個類(例如模型中的安全類")進行驗證更好?或定義一些類進行驗證?你知道更好的選擇或好的技術嗎?
I don't know which one is the best?
do you think it's better to validate user login form
or other forms in controller or it's better to define one class for example 'security class' in model to validation?
or define some classes for validation?
do you know a better choice or good technique?
<?php
class acontroller{
.
.
.
private function loginformAction()
{
$this->actionform='loginform';
$this->errorMsg=array();
if(isset($post)){
if(empty($post('aliasName'))){
...
}else{
...
}
if(empty($post('password'))){
...
}
if(empty($post('re_password'))){
...
}
if(!empty($post('password')) && isset($post('re_password')) ){
...
}
}
$this->render();
}
.
.
.
}
推薦答案
驗證是域邏輯的一部分.控制器應該與此無關.它只需要將傳入的請求值傳遞給模型層的適當部分.
Validation is part of the domain logic. Controller should have nothing to do with this. It only has to pass the incoming request values to the proper parts of model layer.
驗證本身應該發生在模型層內的域對象中.此外,在某些表格中,您必須擔心數據完整性(即注冊表中的唯一用戶名).在這種情況下,數據完整性檢查實際上應該由 數據映射器 處理,本質上,將數據傳遞給SQL 數據庫,執行檢查,如果有違規,則觸發 DB 抽象異常.
The validation itself should happen in domain objects within the model layer. Also, in some forms you have to worry about data integrity (i.e. unique usernames in registration form). In that case the data integrity checks actually should be handled by data mappers by, essentially, passing data to SQL database, which performs the check and, if there is a violation, it triggers an exception on DB abstraction.
由于您的問題與身份驗證/授權有關,您可能會發現這篇文章相關.
Since your problems is dealing with authentication/authorization, you might find this post relevant.
這篇關于在 mvc php 中驗證的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!