Maison  >  Article  >  développement back-end  >  ajax + php + Controller 控制所有后台函数调用

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

WBOY
WBOYoriginal
2016-08-08 09:20:59775parcourir

转载请注明出处: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>?>

以上就介绍了ajax + php + Controller 控制所有后台函数调用,包括了方面的内容,希望对PHP教程有兴趣的朋友有所帮助。

Déclaration:
Le contenu de cet article est volontairement contribué par les internautes et les droits d'auteur appartiennent à l'auteur original. Ce site n'assume aucune responsabilité légale correspondante. Si vous trouvez un contenu suspecté de plagiat ou de contrefaçon, veuillez contacter admin@php.cn