joomla component development with Ajax queries

前端 未结 3 2080
悲&欢浪女
悲&欢浪女 2021-01-15 15:26

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

相关标签:
3条回答
  • 2021-01-15 16:02

    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.

    0 讨论(0)
  • 2021-01-15 16:14

    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.

    1. Create a joomla platform file to include as shown below:
    2. Include that file in your script
    3. 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');
        }
    ?>
    
    0 讨论(0)
  • 2021-01-15 16:17

    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

    0 讨论(0)
提交回复
热议问题