Php 将模型数据从控制器传递到视图joomla 3.1
如何在joomla 3.1中正确地将模型数据从控制器传递到视图。在开始时,我初始化我的一个子控制器方法,以收集项目的数据,该项目应该填充我的表单布局。使用以下url访问。option=com_unis&task=unis。edit&layout=edit&id=1Php 将模型数据从控制器传递到视图joomla 3.1,php,joomla,joomla3.1,data-exchange,Php,Joomla,Joomla3.1,Data Exchange,如何在joomla 3.1中正确地将模型数据从控制器传递到视图。在开始时,我初始化我的一个子控制器方法,以收集项目的数据,该项目应该填充我的表单布局。使用以下url访问。option=com_unis&task=unis。edit&layout=edit&id=1 public function edit() { $input = JFactory::getApplication()->input; $model = $this->getModel ( '
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变量 类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);