Home >Backend Development >PHP Tutorial >ajax + php + Controller controls all background function calls

ajax + php + Controller controls all background function calls

WBOY
WBOYOriginal
2016-08-08 09:20:59776browse

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

I haven’t posted code here for a long time

It is divided into 3 parts to complete the ajax calling logic of php, as follows It is a rough structure

The first part: 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>

The second part: Controller.php, this file is Calling controllers of other specific functional classes 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>?>

Part 3: 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>?>

The above introduces ajax + php + Controller to control all background function calls, including aspects. I hope it will be helpful to friends who are interested in PHP tutorials.

Statement:
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