duanmao2650 2016-03-29 23:17
浏览 62
已采纳

使用AJAX调用的MVC模式

This is a weird question about a php mvc pattern with ajax calls. The purpose is make a better and dynamically web apps. Let me explain you:

When I learn php, I used this pattern specifically :

model.php

<?php
class myClass {

    private $attrOne;
    private $attrTwo;

    public function getAttrOne() {
        return $this->attrOne;
    }

    public function setAttrOne($attrOne) {
        $this->attrOne = $attrOne;
    }

    public function getAttrTwo() {
        return $this->attrTwo;
    }

    public function setAttrTwo($attrTwo) {
        $this->attrTwo = $attrTwo;
    }

    // ----------------------------------------------------

    public function doSelect() {
        //some code
    }

    public function doInsert() {
        //some code
    }

    public function doUpdate() {
        //some code
    }

    public function doDelete() {
        //some code
    }

}

controller.php

<?php

require "../models/model.php";

if(isset($_POST['action'])) {
    $action = $_POST['action'];
    if(is_callable($action)) {
        $action();
    }
}

function registerSomething(){
    $model = new myClass();
    $model->setAttrOne($_POST['attrOne']);
    $model->setAttrTwo($_POST['attrTwo']);
    $return = $model->doInsert();
    echo $return;
}

function registerSomething2(){
    // more app logic code and other stuff
}

view.php -> this is most a pure html file with php extension

<div id="result"></div>
<form id="register" role="form" >
    <input type="text" id="attrOne" name="attrOne"/>
    <input type="text" id="attrTwo" name="attrTwo"/>
</form>

<script src="script.js" type="text/javascript"></script>

And the script.JS

$('#register').submit(function() {
    var action = 'registerSomething';
    $.ajax({
    data: $(this).serialize() + '&action='+action,
    url: '../controlllers/controller.php',
    type: 'POST',
    success: function(response) {
            $('#result').html(response);
        }
    })
    return false;
})

So, what do you think about this pattern? is this pattern efficient? What is the best way to do ajax calls with a proper mvc pattern in php? Is this a best practice?

  • 写回答

1条回答 默认 最新

  • dongyun4010 2016-03-30 05:24
    关注

    If your goal was to specifically implement something MVC-like, then you have utterly failed. This setup has absolutely nothing to do with MVC. To be honest, it seems that you are way too inexperienced for tackling something like that.

    If instead this is your first attempt in applying Separation of Concerns on your code, then it's appropriate. Though, I still wouldn't put that type of code in the production.

    My recommendation for you would be: stop trying to "do MVC" for now, and focus instead on improving your general understanding of web development.

    You should google the following topics for PHP:

    • url routing
    • autoloading and PSR4
    • difference between active record and data mapper pattern
    • request abstraction
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 (希望可以解决问题)ma和mb文件无法正常打开,打开后是空白,但是有正常内存占用,但可以在打开Maya应用程序后打开场景ma和mb格式。
  • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝
  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?
  • ¥15 错误 LNK2001 无法解析的外部符号
  • ¥50 安装pyaudiokits失败
  • ¥15 计组这些题应该咋做呀
  • ¥60 更换迈创SOL6M4AE卡的时候,驱动要重新装才能使用,怎么解决?
  • ¥15 让node服务器有自动加载文件的功能
  • ¥15 jmeter脚本回放有的是对的有的是错的