I'm creating a module where in my main PHP file I've added a reference to a Javascript file. In that Javascript file, I do a jQuery call on dom ready that does an Ajax post to grab the initial content for a div. The URL that it posts to is another PHP file (gateway.php) that is part of my module. (This gateway.php does a call to a remote server to get the content.)
Well, direct access is typically prohibited for module files. I can comment out the line of code that prohibits direct access. Even doing that, for some reason it seems like my gateway.php doesn't have full access to everything in Joomla. For example, gateway.php has a require of my helper.php file. I'm just doing
require_once(dirname(__FILE__) . DS . 'helper.php');
which as far as I can tell is the standard way to include a file. However, I get errors showing that DS isn't getting translated as the value defined within Joomla. Its treating DS like a string value.
If I change it to
require_once(dirname(__FILE__) . '/helper.php');
then I get the direct access prohibited error.
Any idea what's going on here or the correct way to using Ajax in a Joomla module in a file other than the main PHP module file?
Unless there's an specific reason you absolutely need to do otherwise, you should always make your calls through Joomla's index.php file. Don't attempt to call individual PHP files, unless they are 100% independent of Joomla. The calls to
defined( '_JEXEC' ) or die( 'Restricted access' );
at the top of every .php file in Joomla for a reason: you want to minimize the number of entry points into Joomla.To make AJAX/JSON calls that retrieve data from within Joomla, you'll want to create a small component to go along with your module. I wrote a blog post about this a few months back: http://www.designvsdevelop.com/the-way-not-to-do-javascript-in-joomla