Home > Backend Development > PHP Tutorial > ajax php Controller controls all background function calls, ajaxcontroller_PHP tutorial

ajax php Controller controls all background function calls, ajaxcontroller_PHP tutorial

WBOY
Release: 2016-07-13 09:46:32
Original
863 people have browsed it

ajax php Controller controls all background function calls, ajaxcontroller

Please indicate the source for reprinting: http://www.cnblogs.com/ghypnus/p/4645873.html

It’s been a long time since I posted code here

It is divided into 3 parts to complete the ajax calling logic of php. The following is the general structure

Part one: ajax request: mainly the action parameter, LoginController is the class name of php, login is the function name in the LoginController class

$('#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>
Copy after login

Part 2: Controller.php, this file is to call controllers of other specific functional classes and plays a pivotal role, mainly through reflection

<?<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>?>
Copy after login

The third part: LoginController.php, this file is a specific functional class

<?<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>?>
Copy after login

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/1031814.htmlTechArticleajax php Controller controls all background function calls, ajaxcontroller please indicate the source for reprinting: http://www.cnblogs. com/ghypnus/p/4645873.html It’s been a long time since I posted code here...
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 Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template