首頁 > 後端開發 > php教程 > ajax + php + Controller 控制所有後台函數調用

ajax + php + Controller 控制所有後台函數調用

WBOY
發布: 2016-08-08 09:20:59
原創
788 人瀏覽過

轉載請註明出處:http://www.cnblogs.com/ghypnus/p/4645873.html 

好久沒有來這邊發布程式碼了

總共分成3大部分來完成php的ajax呼叫邏輯,以下php是大致的結構

第一部分:ajax請求:主要是action這個參數,LoginController是php的類別名,login是LoginController這個類別中的函數名稱

$('#submit').on('click', <span>function</span><span> (e) {
        e.stopPropagation();
        $.ajax({
            url: </span>"../../controllers/Controller.php"<span>,
            data: {
                action: </span>"LoginController/login"<span>,
                username: username,
                password: password
            },
            dataType: </span>"text"<span>,
            type: </span>'POST'<span>,
            timeout: </span>10000<span>,
            error: </span><span>function</span><span> () {
                alert(</span>"服务器超时"<span>);
            },
            success: </span><span>function</span><span> (data) {
                    alert(data);
            }
        });
    });</span>
登入後複製

第二部分:Controller.php,這個檔案是呼叫其他特定的功能類別的控制器,起到樞紐作用,主要是透過反射來實現

<?<span>php

</span><span>if</span> (!<span>empty</span>(<span>$_REQUEST</span>['action'<span>])) {
    </span><span>try</span><span> {
        </span><span>$action</span> = <span>explode</span>('/', <span>$_REQUEST</span>['action'<span>]);
        </span><span>$class_name</span> = <span>$action</span>[0<span>];
        </span><span>$method_name</span> = <span>$action</span>[1<span>];
        </span><span>require</span><span>$class_name</span> . '.php'<span>;
        </span><span>$class</span> = <span>new</span> ReflectionClass(<span>$class_name</span><span>);
        </span><span>if</span> (<span>class_exists</span>(<span>$class_name</span><span>)) {
            </span><span>if</span> (<span>$class</span>->hasMethod(<span>$method_name</span><span>)) {
                </span><span>$func</span> = <span>$class</span>->getmethod(<span>$method_name</span><span>);
                </span><span>$instance</span> = <span>$class</span>-><span>newInstance();
                </span><span>$func</span>->invokeArgs(<span>$instance</span>, <span>array</span>(<span>$_REQUEST</span><span>));
                </span><span>$result</span> = <span>$instance</span>-><span>getResult();
                </span><span>echo</span><span>$result</span><span>;
            }
        }
    } </span><span>catch</span> (<span>Exception</span><span>$exc</span><span>) {
        </span><span>echo</span><span>$exc</span>-><span>getTraceAsString();
    }
}
</span>?>
登入後複製

第三部分:LoginController.php,這個檔案是具體的功能類別

<?<span>php

</span><span>class</span><span> LoginController {
  <br /><span>private </span>$result;<br /></span><span>function</span><span> LoginController() {
        </span><span>//</span><span>初始化数据库连接等参数</span><span>    }

    </span><span>function</span> login(<span>$args</span><span>) {
       </span><span>//</span><span>具体的登录逻辑</span><span>    }

    </span><span>function</span><span> getResult() {
        </span><span>return</span><span>$this</span>-><span>result;
    }

}

</span>?>
登入後複製

以上就介紹了ajax + php + Controller 控制所有後台函數調用,包括了方面的內容,希望對PHP教程有興趣的朋友有所幫助。

相關標籤:
來源:php.cn
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板