首頁 > 後端開發 > php教程 > ajax + php + Controller 控制所有后台函数调用,ajaxcontroller_PHP教程

ajax + php + Controller 控制所有后台函数调用,ajaxcontroller_PHP教程

WBOY
發布: 2016-07-13 09:46:32
原創
864 人瀏覽過

ajax + php + Controller 控制所有后台函数调用,ajaxcontroller

转载请注明出处:http://www.cnblogs.com/ghypnus/p/4645873.html 

好久没有来这边发布代码了

总共分成3大部分来完成php的ajax调用逻辑,以下是大致的结构

第一部分: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>?>
登入後複製

 

www.bkjia.comtruehttp://www.bkjia.com/PHPjc/1031814.htmlTechArticleajax + php + Controller 控制所有后台函数调用,ajaxcontroller 转载请注明出处:http://www.cnblogs.com/ghypnus/p/4645873.html 好久没有来这边发布代码了 总...
來源:php.cn
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板