繁体   English   中英

将模型数据从控制器传递到视图joomla 3.1

[英]pass models data from controller to the view joomla 3.1

如何在joomla 3.1中将模型数据正确传递给控制器​​中的视图。 在开始时,我初始化我的一个子控制器方法,以收集应该填充我的表单布局的项目的数据。 使用以下url访问?option=com_unis&task=unis.edit&layout=edit&id=1比我的控制器方法看起来像

public function edit() 
{
    $input = JFactory::getApplication()->input;     
    $model = $this->getModel ( 'item'); 
    $view = $this->getView('item', 'html');   
    $view->setModel($model);
    $view->setLayout('edit');

// Display the view
$view->display();

return $this;
}

比如果我尝试在我的视图中访问模型返回null

找到了! 但也许不是最好的解决方法

在视图中我初始化我的模型

$model = $this->getModel('mymodel');
$data  = $model->my_method($args);

与使用公共变量的布局相关联

$this->data = $data;

毕竟我找到了解决方法。 在视图中,我调用我的模型,如下所示

$model = $this->getModel('mymodel');
$data  = $model->my_method($args);

比我创建了一个保存布局数据的公共变量

$this->data = $data;

控制器选择要使用的视图。 可以在views / somefolder / view.html.php中调用模型函数,从那里可以在模板默认文件中查看assignes变量。

class MyPageViewMyPage扩展JViewLegacy {

/**
 * Display the Hello World view
 *
 * @param   string  $tpl  The name of the template file to parse; automatically searches through the template paths.
 *
 * @return  void
 */

public $data;

function display($tpl = null) {
    // Assign data to the view
    $this->msg = $this->get('Msg'); // call a method msg in the model defined

    $model = $this->getModel(); // creating an object for the model

    $this->data = $model->getFilter(1); // call a method defined in model by passing the arguments
    // Check for errors.
    if (count($errors = $this->get('Errors'))) {
        JLog::add(implode('<br />', $errors), JLog::WARNING, 'jerror');

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

}

在默认模板文件中

echo $this->msg;

print_r($this->data);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM