【问题标题】:Joomla the master templateJoomla 主模板
【发布时间】:2014-02-12 13:03:42
【问题描述】:

我正在尝试关注mvc tutorial 并使用扩展管理器安装code

这一切都很好而且花花公子,但我正试图弄清楚页面的所有其余部分来自哪里。模板只打印出“Hello World”,但页面包含菜单和所有内容。

有没有办法只打印出“Hello World”? following 表明我可以编辑一些文件(未指定什么)并让它打印出 JSON,但当输出被某种母版页模板包围时,它将是无效的 JSON。

安装插件后,我有以下文件:

/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>

我可以在 /components/com_helloworld/helloworld.php 中输出一些内容并将其保留在该位置,但我在考虑更多的视图以生成输出和控制器以获取数据。

【问题讨论】:

    标签: model-view-controller joomla2.5


    【解决方案1】:

    添加 &amp;format=json 会更改 jDocument 类型(我猜)并阻止模板被包装在“主”模板中。拥有 /components/com_helloworld/views/helloworld/view.json.php 将生成所需的标头。

    这是因为我有以下文件:/public_html/libraries/joomla/document/json/json.php

    包含以下内容:

    <?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;
        }
    }
    

    我猜你基本上可以在那里添加任何文件并输出任何类型的响应,尽管基础似乎已经存在(也许我会添加 jsonp)。

    【讨论】:

      猜你喜欢
      • 2018-11-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-06-12
      • 1970-01-01
      • 2011-01-29
      • 1970-01-01
      相关资源
      最近更新 更多