Using Extbase for Ajax requests

Extbase and Ajax - no default solution yet

Even as Extbase evolves to become standard in TYPO3 extension development, there is no standard way of handling ajax requests yet. At least no dispatcher is shipped handling eID requests in frontend or ajax requests in backend.

At this point, pt_extbase comes into the game. This little extension can be seen as an addition to Extbase extending it with certain functionality that we need a lot during our extension development at punkt.de. Handling ajax requests is one such requirement.

Daniel describes two possibilities for handling ajax calls - one with a special page type and one with a special ajax dispatcher. This tutorial only concerns the second variation - using a special dispatcher.

Registering your controllers / actions

Registering Extbase controllers and actions to be used with ajax calls is straight forward. Simply use the following lines of code:

// Put the following lines within your ext_localconf.php
Tx_Extbase_Utility_Extension::configurePlugin(
	$_EXTKEY,
	'Pi1',
	array(
		'Ajax' => 'test'
	),
	array(
		'Ajax' => 'test'
	)
);

Registering eID / ajax dispatcher in ext_localconf

After registering your controllers and actions via the snippet offered above, you have to register pt_extbase's ajax dispatcher for handling eID / ajax requests:

// Add the following lines into your ext_localconf.php

if(TYPO3_MODE == 'FE') {
	// For FE usage via eID
	$TYPO3_CONF_VARS['FE']['eID_include']['ptxAjax'] = 
		t3lib_extMgm::extPath('pt_extbase').'Classes/Utility/eIDDispatcher.php';
}

if(TYPO3_MODE == 'BE') {
	// For BE usage via ajax
	$TYPO3_CONF_VARS['BE']['AJAX']['ptxAjax'] = 
		t3lib_extMgm::extPath('pt_extbase').'Classes/Utility/AjaxDispatcher.php:' .
		'Tx_PtExtbase_Utility_AjaxDispatcher->initAndEchoDispatch';
}

Writing Extbase controllers for ajax requests

Your controllers are nothing special. The only thing that differs from "normal" controllers might be the format of the output which most likely won't be HTML but JSON or XML:

<?php

class Tx_TnTests_Controller_AjaxController 
	extends Tx_Extbase_MVC_Controller_ActionController {

	/**
	 * Sample actions returns GET and POST vars in json encoding
	 *
	 * @return string JSON encoded GP vars
	 */
	public function testAction() {
		return json_encode(
			array(
				'GET' => t3lib_div::_GET(),
				'POST' => t3lib_div::_POST()
			)
		);
	}

}
?>

Using Extbase controllers / actions for eID / ajax calls

So the last question is how to use your newly created controller actions for your ajax calls. Here are the sample URLs for usage in frontend and backend:

FRONTEND:
http://<--yourHost-->/?eID=ptxAjax
  &extensionName=<UpperCamelCaseOfExtensionName: pt_extbase --> PtExtbase>
  &pluginName=<Plugin name registered in configurePlugin. Most likely pi1>
  &controllerName=<Controller name without 'controller' at the end ('Ajax' in the above example)>
  &actionName=<Action name without 'action' at the end ('test' in the above example)>

BACKEND:
http://<--yourHost-->/typo3/ajax.php?ajaxID=ptxAjax
  &extensionName=<UpperCamelCaseOfExtensionName: pt_extbase --> PtExtbase>
  &pluginName=<Plugin name registered in configurePlugin. Most likely pi1>
  &controllerName=<Controller name without 'controller' at the end ('Ajax' in the above example)>
  &actionName=<Action name without 'action' at the end ('test' in the above example)>

Further resources on Extbase and Ajax

Please refer to the following resources for further information on Extbase and Ajax within TYPO3:

Daniel's original post: http://daniel.lienert.cc/blog/blog-post/2011/04/23/typo3-extbase-und-ajax/ (German)

TYPO3 Documentation on ajax in backend: href="http://typo3.org/documentation/document-library/core-documentation/doc_core_api/4.2.0/view/3/9/ (English)

Some tutorial on how to use eID within TYPO3: http://www.typo3-tutorials.org/tutorials/extensions/eid-mechanismus.html (German)

 


 
Inhalt © Michael Knoll 2009-2017  •  Powered by TYPO3  •  TypoScript Blogging by Fabrizio Branca  •  TYPO3 Photo Gallery Management by yag  •  Impressum