0

I am new to Joomla and facing a situation - I need to have a page with custom URL (/dashboard) which would return some data in JSON that external application will read. I have a Slideshow CK module, where I need the slides (image url, link) and main menu items (title, url). All returned in one JSON response. Which is the best approach? Create a custom page with it's own template and create a module, which would get data from database (raw sql) and then prepare the desired JSON? Or is there a way how to create a JSON view from Slideshow CK module and menu module?

Thank you all for your tips

0

If you need only a single URL to return a json string containing just a prepared subset of a single module’s params and a copy of all the paths and titles of a single menu then I would suggest a very hard coded approach would be the quickest - a couple of database queries and some data rustling should do it. A system plugin would be suitable place to put the code since you may as well return the json response as early in the life cycle as possible. If it’s literally a single URL you could even test for it in the onBeforeRoute and then you wouldn’t even need a menu item, ie. if this my special URL then grab this data echo a jsonResponse and die.

If on the other hand you actually want something much more dynamic, eg. turning module params into the response depending on which page you’re on then you may be better off making a module template override for any modules you need and then have the modules pass the data through to some custom template that combines the data and returns the json. That would be rather more ... tangly.

0

In my opinion, the best approach is to use a custom plugin, that will be called using com_ajax interface.

It doesn't have to be a 'system' plugin, it can be any group, as it will be specified in url.

for example, you can use the url: index.php?option=com_ajax&group=mycompany&plugin=getmyresponse&format=json

So, it the corresponding plugin would be :

plugins/mycompany/myplugin/myplugin.xml
plugins/mycompany/myplugin/myplugin.php

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

// Import library dependencies
jimport('joomla.plugin.plugin');

final class PlgMycompanyMyplugin extends JPlugin
{
  public function onAjaxGetMyResponse()
  {
    // do your thing
    $data = array('a' => 234);
    echo new \Joomla\CMS\Response\JsonResponse(data);
  }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.