Ajax + PHP + Controller controls all background function calls

Source: Internet
Author: User
Reprint Please specify source: http://www.cnblogs.com/ghypnus/p/4645873.html

It's been a long time since I posted the code here.

A total of 3 to complete the AJAX call logic of PHP, the following is the approximate structure

The first part: AJAX request: The main action of this parameter, Logincontroller is the class name of PHP, login is logincontroller the function name in this class

function (e) {        e.stoppropagation ();        $.ajax ({            ".. /.. /controllers/controller.php ",            data: {                " Logincontroller/login ",                Username:username,                Password:password            },            " Text ",            ' POST ',            10000,            function  () {                alert ( "Server timeout");            },            function  (data) {                    alert (data);            }        );    });

Part II: controller.php, this file is called the controller of other specific function classes, play a pivotal role, mainly through reflection to achieve

 Phpif(!Empty($_request[' Action '])) {    Try {        $action=Explode('/',$_request[' Action ']); $class _name=$action[0]; $method _name=$action[1]; require$class _name. '. php '; $class=NewReflectionclass ($class _name); if(class_exists($class _name)) {            if($class->hasmethod ($method _name)) {                $func=$class->getmethod ($method _name); $instance=$class-newinstance (); $func->invokeargs ($instance,Array($_request)); $result=$instance-GetResult (); Echo$result; }        }    } Catch(Exception$exc) {        Echo$exc-gettraceasstring (); }}?>

Part III: logincontroller.php, this file is specific to the functional class

 
  PHPclass  Logincontroller {
$result;
function Logincontroller () { // Initialize database connection parameters } function Login ($args) { // specific login logic } function GetResult () { return$this,result; }}? >

The above describes the Ajax + PHP + Controller control All background function calls, including aspects of the content, I hope the PHP tutorial interested in a friend helpful.

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.