0
Of course that's possible. I usually create folders in my project such as: Model, View, Static and two PHP files index.php and Controller.php. Also I found out recently that it's better to create one controller for each class but I think that for small learning projects one controller is enough and even for bigger projects it doesn't go against MVC philosophy. So imagine you have html form, from where you want to register new Note.
當然這是可能的。我通常在我的項目中創建文件夾,例如:Model,View,Static和兩個PHP文件index.php和Controller.php。最近我發現最好為每個類創建一個控制器,但我認為對於小型學習項目,一個控制器就足夠了,即使對於更大的項目,它也不會違背MVC理念。所以想象你有html表單,從那里你想要注冊新的Note。
notes.html
ajax.js
function addNote()
{
var formElements = document.querySelectorAll(".form-control");
var formData = new FormData();
formData.append("Code", "1");
formData.append("Sequence", "1");
for(var i = 0; i < formElements.length; i++)
formData.append(formElements[i].name, formElements[i].value);
var xmlHttp = new XMLHttpRequest();
xmlHttp.onreadystatechange = function()
{
if(xmlHttp.readyState == 4 && xmlHttp.status == 200)
{
//doSomething
}
}
xmlHttp.open("POST", "Controller.php");
xmlHttp.send(formData);
}
Controller.php
if(!empty($_POST["Code"] && !empty($_POST["Sequence"]))
{
if($_POST["Code"] == "1")
{
switch($_POST["Sequence"])
{
case 1:
$noteObj = new Note();
$noteObj->create($_POST);
break;
}
}
}
?>
Then you will create Note class, with private fields, accessors and so on. Main idea is to divide your code and make it manageable which means that you can change User-Interface without changing back-end side as you have controller and classes which will return result to all requests, even if you send requests from command line tools such as curl. MVC is as simple as it's main goal and idea. It improves code re-usability and also flexibility in terms of helping you better monitor and upgrade it sequentially.
然后,您將創建Note類,包含私有字段,訪問器等。主要思想是划分你的代碼並使其易於管理,這意味着你可以在不改變后端的情況下改變用戶界面,因為你有控制器和類會將結果返回給所有請求,即使你從命令行工具發送請求如卷曲。 MVC就像它的主要目標和想法一樣簡單。它可以提高代碼的可重用性,並在幫助您按順序監視和升級代碼方面提供靈活性。