php use和using,是否可以使用Jquery AJAX和PHP使用MVC架構?

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就像它的主要目標和想法一樣簡單。它可以提高代碼的可重用性,並在幫助您按順序監視​​和升級代碼方面提供靈活性。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值