2011-03-16 76 views
12

我是Joomla的新手,我想知道Joomla控制器如何將數據傳遞給模型,模型以控制器和控制器來查看。雖然這可能是一個愚蠢的問題,但我真的試圖找到答案。我希望我能從stackoverflow系列獲得一些幫助。Joomla模型視圖控制器(MVC)如何工作?

+0

順便說一句MVC代表模型視圖控制器 – Martin 2011-04-19 08:32:01

回答

29

控制器拾取在url視圖變量和使用這些確定哪個視圖需要被使用。然後它設置要使用的視圖。該視圖然後調用模型來獲取它所需的數據,然後將其傳遞給tmpl以進行顯示。

下面是這一切是如何一起工作進行簡單的設置:

組件/ com_test/Controller.php這樣

class TestController extends JController 
{ 

    // default view 
    function display() { 
    // gets the variable some_var if it was posted or passed view GET. 
    $var = JRequest::getVar('some_var'); 
    // sets the view to someview.html.php 
    $view = & $this->getView('someview', 'html'); 
    // sets the template to someview.php 
    $viewLayout = JRequest::getVar('tmpl', 'someviewtmpl'); 
    // assigns the right model (someview.php) to the view 
    if ($model = & $this->getModel('someview')) $view->setModel($model, true); 
    // tell the view which tmpl to use 
    $view->setLayout($viewLayout); 
    // go off to the view and call the displaySomeView() method, also pass in $var variable 
    $view->displaySomeView($var); 
    } 

} 

組件/ com_test /視圖/ someview/view.html.php

class EatViewSomeView extends JView 
{ 

    function displaySomeView($var) { 
    // fetch the model assigned to this view by the controller 
    $model = $this->getModel(); 
    // use the model to get the data we want to use on the frontend tmpl 
    $data = $model->getSomeInfo($var); 
    // assign model results to view tmpl 
    $this->assignRef('data', $data); 
    // call the parent class constructor in order to display the tmpl 
    parent::display(); 
    } 

} 

組件/ com_test /模型/ someview.php

class EatModelSomeView extends JModel 
{ 

    // fetch the info from the database 
    function getSomeInfo($var) { 
    // get the database object 
    $db = $this->getDBO(); 
    // run this query 
    $db->setQuery(" 
     SELECT 
     * 
     FROM #__some_table 
     WHERE column=$var 
    "); 
    // return the results as an array of objects which represent each row in the results set from mysql select 
    return $db->loadObjectList(); 
    } 

} 

組件/ com_test /視圖/ someview/TMPL/someviewtmpl.php

// loop through the results passed to us in the tmpl 
foreach($this->data as $data) { 
    // each step here is a row and we can access the data in this row for each column by 
    // using $data->[col_name] where [col_name] is the name of the column you have in your db 
    echo $data->column_name; 
} 
+0

controller.php'$ var = JRequest :: getVar('some_var');',它從哪裏得到'some_var'?它來自編碼的網址嗎? – Plummer 2012-11-27 17:15:29

+1

'index.php?option = com_test&view = someview&some_var = 1234' – Martin 2012-11-30 07:49:08

+0

所以。 Joomla是與模型交互以獲取數據的視圖嗎? – Nobita 2014-02-07 23:28:24

2

查看本網站的詳細教程,瞭解如何使用Joomla的MVC製作組件和模塊。希望它有助於

https://docs.joomla.org/Developing_a_MVC_Component

+1

死鏈接......可能不是當你發佈所以沒有降票時。 – araisbec 2014-02-18 14:29:05

+3

每當你連接到其他地方時,請張貼鏈接的回顧。 – 2014-07-24 16:19:06

相關問題