我正在建設我的MVC和樹枝的PHP項目,我有行動'輸入'表單驗證後返回一個變量字符串的問題。 這裏是我的控制文件:樹枝與MVC - 如何獲得變量運行後的動作文件
這裏是我的index.php
文件,該文件被顯示的索引頁:
// '/class/index.php' file with class called 'Index'
class Index {
private $lang;
private $db;
public function __construct($db, $lang) {
$this->lang = $lang;
$this->db = $db;
}
public function generate() {
// new Core object
$core = new BlogCore($this->db);
/*
** pagination
*/
$numRows = $core->getNumRows();
$pagination = new Pagination($_GET['p']);
$pagination->setNumRows($numRows);
$pagination->setLimit(3); // set 3 posts on one page
$limit = $pagination->sqlPagination();
$offset = $pagination->sqlPaginationOffset($limit);
/*
** select detalis form database
*/
$posts = $core->select($limit, $offset);
return [
"posts" => $posts,
"pagination" => $pagination,
"lang" => $this->lang
];
}
public function getTemplate() {
return "default.tpl";
}
}
這裏是insert.php
文件,該文件是在行動insert
(就在我想用從表單輸入新的數據到我的分貝)。在這裏,我正在用正則表達式檢查填充在字段中的數據是否正確。如果沒有,在這裏我想返回模板我的變量,這將顯示用戶通知。
// '/class/insert.php' file with 'Insert' class
class Insert {
private $lang;
private $db;
private $postText;
private $fileName;
private $myFile;
private $phoneValidate;
private $urlValidate;
public function __construct($db, $lang) {
$this->lang = $lang;
$this->db = $db;
}
public function generate() {
$postText = $_POST["postText"];
$myFile = $_FILES["myFile"];
$fileName = $myFile["name"];
$phoneNum = $_POST['phoneNumber'];
$url = $_POST['url'];
$validate = new Validation;
$phoneValidate = "test";
// phoneValidate for form validation
if (!$validate->phoneNum(isset($phoneNum))) {
$phoneValidate = "error";
} else {
$phoneValidate = "ok";
}
// new Core object
$core = new BlogCore($this->db);
// do insert in SQL
if (!empty($postText)) {
$core->insert($postText, $fileName);
$core->saveFile($myFile);
}
header("Location: /codeme/06_blog_v5_mvc/index.php");
}
public function getTemplate() {
return null;
}
}
所以,問題是:我怎麼能修改此文件,以獲得我的索引文件(像我這樣做實際上header()
功能,讓所謂的$phoneValidate
如果您需要變量的附加價值,這個項目?在我的帳戶到位桶共享: https://bitbucket.org/Pirum/simple-blog/src