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 genericvar 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";
$myUrl = Config::$siteRoot . Framework::getComponentAjaxDir("datagrid") . "/getsomedata.php";
?>
$("#ajax").load(<?=json_encode($myUrl)?>);
, include framework.php... , ".js.php" .
- ?