简体   繁体   中英

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.
Is it possible to create one single php file and than on jquery side refer to a specific function in the php file?
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).

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:

$.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. 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

$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. Wouldn't want someone to send 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:

JS

$.ajax({
    type: 'POST',
    url: 'function.php',
    data: {
        action: 'function01',
        ... // Other Data
    }
    ...
});

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. You do yourself a favor as you are now able to only define 1 error handler, 1 output handler 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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM