问题
I have a PHP script used for AJAX queries, but I want them to be able to operate under the umbrella of Joomla's (2.5) framework so I can have session id's, user id's etc available to me.
For example:
$(function () {
$.ajax({
url: 'ajax.php', //the script to call to get data
dataType: 'json' //data format
...
});
});
Where ajax.php has code such as:
$user =& JFactory::getUser();
From what I understand it's best to make your AJAX/JSON calls to a standard Joomla component. I don't know much about developing a MVC component but from what I can see it is way overkill for what I want to do.
Is there something else I could be using?
回答1:
if you create a component you can create new view
for raw queries for example compoments/com_yourcomponent/views/ajax/view.raw.php
and put all logic and output in there
url will be index.php?option=com_yourcomponent&view=ajax&format=raw
or
you can to create new method in controller.php
with exit()
after print information and url will be index.php?option=com_yourcomponent&task=ajax
回答2:
This is absolutely possible by way of the Joomla Platform. The example I'll give you below is actually for J1.5, but is easily adaptable to J2.5 with some adjustment to the included files.
- Create a joomla platform file to include as shown below:
- Include that file in your script
- Use the now-available Joomla environment for your functions.
Another strong recommendation is to implement a ReSTful API instead of your custom scripts. It's outrageously simple using Luracast Restler. I had it up and running in about 10 minutes, then added the Joomla Framework as shown below, and had an extremely flexible Joomla based API using AJAX calls for my site in under an hour! Best spent development time in years, as far as I'm concerned.
yourscript.php
require_once('joomla_platform.php');
/* Get some of the available Joomla stuff */
$config = new JConfig();
$db = &JFactory::getDBO();
$user =& JFactory::getUser();
if($user->gid <25) {
die ("YOU CANT BE HERE");
}
echo "<pre>".print_r($config,true)."</pre>";
joomla_platform.php
<?php
/* Initialize Joomla framework */
if (!defined('_JEXEC')) {
define( '_JEXEC', 1 );
// define('JPATH_BASE', dirname(__FILE__) );
define ('JPATH_BASE', "c:\\wamp\\www");
define( 'DS', DIRECTORY_SEPARATOR );
/* Required Files */
require_once ( JPATH_BASE .DS.'includes'.DS.'defines.php' );
require_once ( JPATH_BASE .DS.'includes'.DS.'framework.php' );
/* To use Joomla's Database Class */
require_once ( JPATH_BASE .DS.'libraries'.DS.'joomla'.DS.'factory.php' );
require_once ( JPATH_LIBRARIES.DS.'joomla'.DS.'import.php'); // Joomla library imports.
/* Create the Application */
global $mainframe;
$mainframe =& JFactory::getApplication('site');
}
?>
回答3:
You don't need to create any custom files and add them into Joomla script. You just need a controller to serve ajax request. You don't even need a view (one way).
Your ajax call should be like these:
$(function () {
$.ajax({
url: 'index.php?option=com_<component_name>&no_html=1task=<controller_name>.<controller_action>', //not_html = 1 is important since joomla always renders it's default layout with menus and everything else, but we want the raw json output
dataType: 'json' //data format
...
});
});
And your controller:
/*
* @file admin/controller/<controller_name>.php
*/
class <component_name>Controller<controller_name> extends JController
{
public function <controller_action>()
{
//do something
$respnse['message'] = 'Your message for the view';
die(json_encode($reponse));
}
}
...
This is just one of the examples of how it's could be done.
来源:https://stackoverflow.com/questions/11454226/joomla-component-development-with-ajax-queries