MVC php将模型传递给控制器 [英] MVC php passing model to controller
问题描述
我有这个登录系统,当我将模型链接到我的控制器时,控制器无法获取模型的变量 $username、$password 等.如何正确处理这个模型和控制器?我没有使用任何框架,只是纯粹的 php,只是遵循 mvc 模式.
I have this login system, when I link the model to my controller, the controller can't get the variables of model which is $username, $password etc. How to properly way of handling this model and controller? I'm not using any framework just pure php and just following the mvc pattern.
当我运行代码时,它会转到无效的用户名或密码
When I run the code it goes to Invalid Username or Password
型号
require_once("db.php");
class loginModel{
public function __construct(){
$dbCon = new DbConnector();
$this->dbCon = $dbCon->getConnection();
}
public function queryUser($username, $password){ // use parameters
$username = mysqli_real_escape_string($this->dbCon, $username);
$password = mysqli_real_escape_string($this->dbCon, sha1($password));
$empty = "da39a3ee5e6b4b0d3255bfef95601890afd80709";
$myQuery = "SELECT * FROM login WHERE username = '".$username."' and password = '".$password."'";
$results = $this->dbCon->query($myQuery);
return $results->fetch_array(); // return the result
if(empty($username) or $password == $empty){
return "Username or Password is empty";
}elseif($username == $row['username'] and $password == $row['password']){
if($results->num_rows == 1){
session_start();
$_SESSION['user'] = $username;
header('Location: ../view/index.php');
}else{
return "Login Unsuccessful";
}
}else{
return "Invalid Username or Password";
}
}
}
控制器
require_once("../model/loginModel.php");
class loginController{
public $loginModel;
public function __construct(){
$loginModel = new loginModel(); // create object of model
}
public function select(){
$userData = $this->loginModel->queryUser($_POST['username'], $_POST['password']);
if($userData) {
session_start();
$_SESSION['user'] = $userData;
header('Location: ../view/index.php');
} else {
return "Invalid Username or Password";
}
}
}
$loginController = new loginController();
$loginController->select();
推荐答案
你的代码有很多问题.
第一: empty($username)
将始终返回 false,因为您尚未在该函数中定义 $username.
First: empty($username)
will always return false as you have not defined $username in that function.
第二:变量 $row['username']
在不同的函数中你不能访问外部的局部变量.
Second: variable $row['username']
in different function you cant access local variable outside.
require_once("db.php");
class loginModel{
public function __construct(){
$dbCon = new DbConnector();
$this->dbCon = $dbCon->getConnection();
}
public function queryUser($username, $password){ // use parameters
$username = mysqli_real_escape_string($this->dbCon, $username);
$password = mysqli_real_escape_string($this->dbCon, sha1($password));
$empty = "da39a3ee5e6b4b0d3255bfef95601890afd80709";
$myQuery = "SELECT * FROM login WHERE username = '".$username."' and password = '".$password."'";
$results = $this->dbCon->query($myQuery);
return $results->fetch_array(); // return the result
// do the validation and exception handling too
}
}
控制器:
//require_once("../model/db.php"); No need to imprt this
require_once("../model/loginModel.php");
class loginController{
$loginModel;
public function __construct(){
$loginModel = new loginModel(); // create object of model
}
public function select(){
$userData = $this->loginModel->queryUser($_POST['username'], $_POST['password']);
if($userData) {
session_start();
$_SESSION['user'] = $userData;
header('Location: ../view/index.php');
} else {
return "Invalid Username or Password";
}
}
}
这篇关于MVC php将模型传递给控制器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!