Home > php教程 > php手册 > ajax+php控制所有后台函数调用,ajaxphp后台函数

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

WBOY
Release: 2016-06-13 08:57:49
Original
1124 people have browsed it

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

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

第一部分:ajax请求:主要是action这个参数,LoginController是php的类名,login是LoginController这个类中的函数名

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

Copy after login

第二部分:Controller.php,这个文件是调用其他具体的功能类的控制器,起到枢纽作用,主要是通过反射来实现

<&#63;php

if (!empty($_REQUEST['action'])) {
  try {
    $action = explode('/', $_REQUEST['action']);
    $class_name = $action[0];
    $method_name = $action[1];
    require $class_name . '.php';
    $class = new ReflectionClass($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();
  }
}
&#63;>

Copy after login

第三部分:LoginController.php,这个文件是具体的功能类

<&#63;php
class LoginController {
  
  private $result;
  function LoginController() {
    //初始化数据库连接等参数
  }
  function login($args) {
    //具体的登录逻辑
  }
  function getResult() {
    return $this->result;
  }
}
&#63;>

Copy after login

意思所述就是本文的全部内容了,希望大家能够喜欢。

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template