0
votes

I'm trying to follow the mvc tutorial and installed the code with extension manager.

It all works fine and dandy but I'm trying to figure out where all the rest of the page comes from. The template only prints out "Hello World" but the page is complete with menu and all.

Is there a way to only print out "Hello World"? The following shows that I can edit some file (not specified what) and have it print out JSON, it'll be invalid JSON when the output is surrounded with some sort of master page template though.

After installing the plugin I have the following files:

/components/com_helloworld/helloworld.php

<?php

// No direct access to this file
defined('_JEXEC') or die('Restricted access');

// import joomla controller library
jimport('joomla.application.component.controller');

// Get an instance of the controller prefixed by HelloWorld
$controller = JController::getInstance('HelloWorld');

// Perform the Request task
$controller->execute(JRequest::getCmd('task'));

// Redirect if set by the controller
$controller->redirect();

/components/com_helloworld/controller.php

<?php
// No direct access to this file
defined('_JEXEC') or die('Restricted access');

// import Joomla controller library
jimport('joomla.application.component.controller');

/**
 * Hello World Component Controller
 */
class HelloWorldController extends JController
{
}

/components/com_helloworld/views/helloworld/view.html.php

<?php
// No direct access to this file
defined('_JEXEC') or die('Restricted access');

// import Joomla view library
jimport('joomla.application.component.view');

/**
 * HTML View class for the HelloWorld Component
 */
class HelloWorldViewHelloWorld extends JView
{
    // Overwriting JView display method
    function display($tpl = null) 
    {
        // Assign data to the view
        $this->msg = 'Hello World';

        // Display the view
        parent::display($tpl);
    }
}

/components/com_helloworld/views/helloworld/tmpl/default.php

<?php
// No direct access to this file
defined('_JEXEC') or die('Restricted access');
?>
<h1><?php echo $this->msg; ?></h1>

I can output something in /components/com_helloworld/helloworld.php and leave it at that but was thinking more along the way of the view to produce the output and the controller to fetch the data.

1

1 Answers

0
votes

Adding &format=json changes the jDocument type (I guess) and stops the template from being wrapped in a "master" template. Having /components/com_helloworld/views/helloworld/view.json.php will produce the needed headers.

This works because I have the following file: /public_html/libraries/joomla/document/json/json.php

Containing the following:

<?php
defined('JPATH_PLATFORM') or die;
class JDocumentJSON extends JDocument
{
    protected $_name = 'joomla';
    public function __construct($options = array())
    {
        parent::__construct($options);

        // Set mime type
        $this->_mime = 'application/json';

        // Set document type
        $this->_type = 'json';
    }
    public function render($cache = false, $params = array())
    {
        JResponse::allowCache(false);
        JResponse::setHeader('Content-disposition', 'attachment; filename="' . $this->getName() . '.json"', true);

        parent::render();

        return $this->getBuffer();
    }
    public function getName()
    {
        return $this->_name;
    }
    public function setName($name = 'joomla')
    {
        $this->_name = $name;

        return $this;
    }
}

My guess is you can add basically any file there and output any type of response although the basics seem to be there already (maybe I will add jsonp).