[英]one php file for all jquery post requests?
I have a lot of jquery ajax methods and for each of them a small php file. 我有很多jquery ajax方法,并且每个方法都有一个小的php文件。
Is it possible to create one single php file and than on jquery side refer to a specific function in the php file? 是否可以创建一个单独的php文件,而不是在jquery方面引用php文件中的特定函数?
Like this: 像这样:
$.ajax({
type: "POST",
url: "functions.php", function01(),
....
next function: 下一个功能:
$.ajax({
type: "POST",
url: "functions.php", function02(),
....
And if it is possible - is it maybe a wrong practice for overall performance? 如果有可能 - 整体表现可能是错误的做法吗?
You are looking for routing the requests to the right recipients. 您正在寻找将请求路由到正确的收件人。
This is a common practice among many MVC frameworks and it does not have notable performance impacts in contrast to maintainability of the code (– if done right). 这是许多MVC框架中的常见做法,与代码的可维护性相比,它没有显着的性能影响( - 如果做得对)。
A very simplified example: 一个非常简单的例子:
<?php
// request.php
$allowedRequests = [
// these files will correctly handle the specific requests when included
'db' => 'db.php',
'file' => 'file.php'
];
$request = $_GET['request'];
if (isset($allowedRequests[$request)) {
include($allowedRequests[$request]);
}
Then, just pass another GET parameter on the client side: 然后,只需在客户端传递另一个GET参数:
$.ajax({
type: "POST",
url: "functions.php?request=db"
Depending on the request parameter, the correct file will be chosen and included. 根据请求参数,将选择并包含正确的文件。
Note: As I already said, this is a very simplified example. 注意:正如我已经说过的,这是一个非常简单的例子。 For instance, CakePHP supports storing grouped functionality (=controllers) in different files. 例如,CakePHP支持在不同文件中存储分组功能(=控制器)。 Anyway, the gist is to redirect the whole request parameters to the correct part of your application. 无论如何,要点是将整个请求参数重定向到应用程序的正确部分。
You can, but not quite like that. 你可以,但不太喜欢。
I suggest sending a parameter in the request that tells it what function to run. 我建议在请求中发送一个参数来告诉它要运行什么函数。
$.ajax({
type: "POST",
url: "functions.php",
data: {
method: 'function01'
# and then whatever other data you send
}
});
Then in your PHP, just use the method
param to call the right function. 然后在PHP中,只需使用method
param来调用正确的函数。
<?php
$funcName = $_POST['method'];
call_user_func($funcName);
function function01(){
}
function function02(){
}
Note: You should probably check $_POST['method']
against a white-list so that your page is secure. 注意:你应该对白名单检查$_POST['method']
,这样你的页面就是安全的。 Wouldn't want someone to send method=eval
. 不希望有人发送method=eval
。
You could pass a $_POST var along with the rest of the AJAX data and use it to determine the function to run like so: 您可以传递$ _POST var以及其余的AJAX数据,并使用它来确定要运行的函数,如下所示:
JS JS
$.ajax({
type: 'POST',
url: 'function.php',
data: {
action: 'function01',
... // Other Data
}
...
});
PHP PHP
if ($_POST['action'] == 'function01'){
...
}
I hope this helps! 我希望这有帮助!
Your right, in fact you should try not to do it in another way. 你的权利,事实上你应该尽量不以另一种方式去做。 Think about 1 dispatcher file which calls the correct function. 想一下调用正确函数的1个调度程序文件。 You do yourself a favor as you are now able to only define 1 error handler, 1 output handler etcetc. 你自己帮个忙,因为你现在只能定义1个错误处理程序,1个输出处理程序etcetc。
As for your question: add 'data' to your request so you can identify the type of request. 至于您的问题:在您的请求中添加“数据”,以便您可以识别请求的类型。 Depending on the type of request you can call the correct method( for example, with a switch to evaluate a $_POST value) 根据请求的类型,您可以调用正确的方法(例如,使用开关来评估$ _POST值)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.