【问题标题】:joomla mvc model results where ate they called?joomla mvc 模型结果它们在哪里调用?
【发布时间】:2013-04-27 23:49:04
【问题描述】:

我很困惑如何在 joomla 2.5 中投影模型结果

我有正在初始化模型的控制器

class FrontpageMyComponentControllerItem extends JControllerLegacy
{

    private $id;

    public function display($cachable = false, $urlparams = array())
    {
        // Initialise variables.
        $jinput = JFactory::getApplication()->input;
                $this->id = $jinput->get('id');
        $cachable = true;

                $model = $this->getModel('item');
                $result_in_view = $model->Item('23'); //$id what I get

        // Set the default view name and format from the Request.
        $viewName = $jinput->get('view', 'item');
        $jinput->set('view', $viewName); 

        return parent::display($cachable, $safeurlparams);
    }

}

现在如何在我的视图中显示结果?

【问题讨论】:

    标签: oop model-view-controller joomla


    【解决方案1】:

    在大多数组件中,数据(结果)是由 ViewModel 检索的。

    我不太确定为什么,但我想这是为了给 Views 更多的权力。

    FrontpageMyComponentViewItem extends JViewLegacy
    {
        /** @var  array  Data are stored here */
        public $items;
    
        public function display($tpl = null)
        {
            /** Retrieve data from Model */
            $items = $this->get('Items');
    
            // Check for errors.
            if (count($errors = $this->get('Errors')))
            {
                // Raise an error
                JError::raiseWarning(500, implode("\n", $errors));
    
                return false;
            }
    
            // Assign data, so layout can access these
            $this->items =& $items;
    
            parent::display($tpl);
        }
    }
    

    这与通常的 MVC 实现不同,其中 ControllerModel 检索数据并将其传递给 View

    如果您想在 Joomla 中查看此示例,请查看 MediaController in Joomla 2.5

    有时可能不需要 View(输出为 JSON),然后您可以在控制器中检索数据并使用 echo 立即输出,就像在 search suggestions 中一样

    Joomla 中有新的 MVC(与旧版相比)类,但核心组件仍然不使用它们。

    【讨论】:

      猜你喜欢
      • 2011-01-02
      • 1970-01-01
      • 2014-04-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-12-07
      • 2013-06-28
      相关资源
      最近更新 更多