AJAX context-sensitive call on a modular site

Edit: I was thinking of a possible solution, but I asked another question, since it is very specific: see AJAX proxies with PHP, is this possible?


A couple of times I ran into this problem ...

I create sites that have a certain degree of modularity. Thus, it is possible that there are “components” (think of crude CMS) that carry their own PHP code, CSS, and JavaScript, all dynamically included. Think of a structure like:

{siteroot}/component/datagrid/datagrid.php
{siteroot}/component/datagrid/js/datagrid.js
{siteroot}/component/datagrid/css/datagrid.css
{siteroot}/component/datagrid/ajax/getsomedata.php

Now the question is: for JavaScript files and, above all, AJAX calls, how can I make them context-sensitive with URLs? For example, if in datagrid.jsI want to call siteroot/component/datagrid/ajax/getsomedata.phpwith AJAX, I have to write (using jQuery):

$("#ajax").load("siteroot/component/datagrid/ajax/getsomedata.php");


First problem : siterootchanges on different settings. I did this by turning on the generic
var codeBase = <? echo json_encode(Config::$siteRoot); ?>

with PHP on each page, from a Config file, which can be easily edited for each installation, so I can do with any JavaScript, for example:

$("#ajax").load(codeBase + "/component/Datagrid/ajax/getsomedata.php");

What do you think of this approach?


The second problem : but I have PHP functions that return me also the component folder or the folder of other components. It would be nice to make a dynamic whole . It would also allow for changes in the structure of the component if I wanted. The only solution I found is to use dynamic Javascript .js.php. This is very elusive, and I have to include the whole structure in a JavaScript file, for example:
<?php
include "../../libs/framework.php"; // get my functions...
$myUrl = Config::$siteRoot . Framework::getComponentAjaxDir("datagrid") . "/getsomedata.php";
?>
$("#ajax").load(<?=json_encode($myUrl)?>);

, include framework.php... , ".js.php" . - ?

+5
2

, , , , . :

  • - AJAX , codeBase - JavaScript
  • : plugin action, a) , "" ajax, b) ajax, :

    $( "#..." ). load (codeBase + "/main/ajax.php?plugin=Datagrid&action=gettable&otherparams"...)

  • ajax.php plugin action "" ajax :

    {ServerRoot}//{}/Ajax/{}.php

include ajax.php

+1

, problems options .

.

, 1 config.php , modules, .. datagrid ..

:

$_SITE_PATH = "/var/www/html/";
$_HTTP_PATH = "http://example.com/";

$_MODULES_PATH = $_SITE_PATH."modules/"

$_MODULES = array(
    "datagrid"=>$_MODULES_PATH."datagrid/init.php",
    "something_else"=>$_MODULES_PATH."something_else/init.php"
);

module init.php, , .

, , , - ( ), - .

global $_MODULES;
require_once($_MODULES["datagrid"]);

- variable .

+1

All Articles