Joomla! AJAX Interface

By Matt Thomas (a.k.a betweenbrain)

Now in core (yay!)

Included in Joomla 3.2 core distribution

Joomla 1.5 and 2.5 versions available https://github.com/Joomla-Ajax-Interface

Origin

Inspired by a client, fostered by community.

Resolves limitation of requests to modules and plugins.

AJAX Defined

A group of web technologies to send and retrieve data from a server asynchronously without interfering with the page.

AJAX (jQuery)


(function ($) {
    $.ajax({
        data   : {
            'option' : 'com_ajax',
            'plugin' : 'latestarticles',
            'format' : 'json'
        },
        success: function (response) {
            console.log(response)
        }
    });
})(jQuery)
						

The Challenge

A Joomla request must always be a component, can never be a module or plugin.

The Solution

Make requests to modules and plugins via a component.

Ajax Request


?option=com_ajax&plugin=latestarticles&format=json
						

Anatomy of an Ajax Request

Required

Call the component

option=com_ajax

Designate the target

[module|plugin]=name
(e.g. module=session for mod_session)

Response format

format=[json|debug|raw]

Module Variable

$module     = $input->get('module');            [e.g. module=session]

Path of helper file

$helperFile = JPATH_BASE . '/modules/mod_' . $module . '/helper.php';

Class name

$class      = 'mod' . ucfirst($module) . 'Helper';

Optional

Alternate module method prefix.

method=[custom fragment]
(defaults to 'get', suffix always 'Ajax', e.g. 'getAjax')

&module=session

File Structure


└── modules
  └── mod_session
    ├── helper.php
    ├── mod_session.php
    ├── mod_session.xml
    └── tmpl
      └── default.php
						

helper.php


<?php defined('_JEXEC') or die;

class modSessionHelper {

	public static function getAjax() {

	}
}
						

Plugin Variable

$plugin     = ucfirst($input->get('plugin'));

Triggered plugin event

$response   = $dispatcher->trigger('onAjax' . $plugin);

&plugin=latestarticles

File Structure


└── plugins
  └── ajax
    └── latestarticles
	 ├── latestarticles.php
	 └── latestarticles.xml

latestarticles.php


<?php defined('_JEXEC') or die;

class plgAjaxLatestarticles extends JPlugin {

	function onAjaxLatestarticles() {

	}
}
						

Additional Data

Include anything else your plugin or module needs.

cmd=add
data=foo
debug=true

Code

Joomla 3.2+ version of component in core.

Joomla 1.5, 2.5 version of component and samples https://github.com/Joomla-Ajax-Interface.

Thank You

Matt Thomas (a.k.a betweenbrain)

http://betweenbrain.com/talks/ajax-interface/

https://github.com/Joomla-Ajax-Interface