I have a custom Joomla 3.4.1 component which has a view that displays data either in JSON format (from views/myview/view.json.php file) or HTML (from views/myview/view.html.php file) format. Is there a way how to change the default view's format from HTML to JSON so that
http://example.com/component/mycomponent/myview
returns JSON data instead of HTML while:
http://example.com/component/mycomponent/myview?format=html
still returns the HTML template of the view?
EDIT:
As far as I get is this router:
function TouristGuideParseRoute($segments) {
$count = count($segments);
$parameters = array();
if ($count > 0) {
$parameters['view'] = $segments[0];
}
if ($count > 1) {
$parameters['task'] = $segments[1];
}
if ($count > 2) {
$parameters['id'] = $segments[2];
}
if ($count > 3) {
$parameters['format'] = $segments[3];
}
if (($parameters['view'] == 'api') && empty($parameters['format'])) {
$application = JFactory::getApplication();
$input = $application->input;
$parameters['format'] = $input->getString('format', 'json');
}
return $parameters;
}
which displays JSON format even if URL contains ?format=html
because in this router the $application->input
is empty (probably populated later on in Joomla request processing chain) and thus $input->getString('format','json')
returns json
all the time.
In your controller, set your default format manually like this (Joomla 3)
$input = JFactory::getDocument()->input;
$format = $input->get('format','json','STR');
$view = $this->getView($view_name, $format);
$view->display($tpl);
Hope this can help.
If your custom component has a router (as I would assume from your example urls) that is where you'd set the default to be JSON.
In the PARSE function you'll want to set...
$vars['format'] = 'json';
Take a look at this semi-current tutorial on Joomla Docs . Then/Or do something like...
$jinput = JFactory::getApplication()->input;
$outputFormat = $jinput->getString('format', 'json');
$vars['format'] = ($outputFormat == 'json') ? 'json' : 'html';`
Note that I'm using an intermediate variable and checking it against the default (line 2) so that it is constrained to two specific options (a user cant type ?format=blahblah
and have the router get messed up with $vars['format'] = 'blahblah'
.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.