[英]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.