Joomla Component MVC变量在View和Controller之间传递

时间:2012-10-31 14:19:06

标签: php model-view-controller joomla

我对Joomla和他们实施MVC arcitecture的方式相对较新。它与我习惯它(Code Igniter等)的方式略有不同,我只是将数据传递给视图时遇到了一些问题。

我在控制器中创建了一个名为'getInitClubs'的函数,它可以在页面运行时自动运行,因此我从'view.html'文件调用了这个函数。

这是我的代码:

控制器功能:

public function getInitClubs() {
        $model =& $this->getModel('directory');
        $init_clubs = $model->clubResults($conds = array());
        return $init_clubs;
    }

模型功能:

public function clubResults($conds) {
    $query = "SELECT * FROM vyj20_contact_details";
    if(isset($conds['city'])) {
        $query .= " WHERE state = '" . mysql_escape_string($conds['city']) . "'";
    }
    if(isset($conds['suburb'])) {
        $query .= " AND suburb = '" . mysql_escape_string($conds['suburb']) . "'";
    }
    $this->_db->setQuery($query);
    $results = $this->_db->loadObjectList();
    return $results;
}

现在我知道模型和控制器代码有效,这不是问题,问题实际上是从控制器函数'getInitClubs'中提取结果并通过视图显示它们。

如何从视图中调用控制器功能(就像我在Code Igniter MVC上那样)以收集显示结果?我尝试过但尝试过但似乎无法弄明白! 感谢

2 个答案:

答案 0 :(得分:1)

JView个实例有getModel()方法。您将获得访问static registry的模型实例,该实例在所有组件之间共享。在JController实例中,您应该更改该模型的状态,并在JView实例中删除数据。

MVC设计模式中的视图应该从模型层获取信息并使用模板呈现它,或者在某些情况下,只发送一个简单的HTTP头。在这方面,Joomla对MVC模式有着惊人的好处。

  

P.S。自PHP 5.0发布以来(大约在2005年左右),在分配对象时不需要使用引用。它实际上是有害的,因为它与引用计数混淆。

答案 1 :(得分:0)

你的控制器功能应该是。

$model =& $this->getModel('directory');
$init_clubs = $model->clubResults($conds = array());
$view = $this->getView('view_name', 'html'); // get the view
$view->assignRef('data_list', $init_clubs); // set the data to the view
$view->display(); // show the view

了解更多

Developing a Model-View-Controller Component

Model-View-Controller

Developing a Model-View-Controller Component/

Model View Controller