2011-03-16 5 views
12

저는 Joomla를 처음 사용하기 때문에 Joomla 컨트롤러가 모델에 데이터를 전달하고 모델을 컨트롤러와 컨트롤러로 전달하는 방법을 알고 싶습니다. 이것은 어리석은 질문 일지 모르지만, 나는 그 답을 찾기 위해 정말로 노력했다. 나는 stackoverflow 가족으로부터 도움을 얻을 수 있기를 바랍니다.Joomla MVC (Model View Controller)는 어떻게 작동합니까?

+0

BTW MVC는 모델보기 컨트롤러의 약자입니다 – Martin

답변

29

도움이되기를 바랍니다. 그런 다음 사용할보기를 설정합니다. 뷰는 모델을 호출하여 필요한 데이터를 가져온 다음이를 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'를 얻을 수 있습니까? 인코딩 된 URL에서 가져 왔습니까? – Plummer

+1

'index.php? option = com_test & view = someview & some_var = 1234' – Martin

+0

그럼. Joomla에서는 데이터를 얻기 위해 모델과 상호 작용하는 뷰가 있습니까? – Nobita

2

Joomla의 MVC를 사용하여 구성 요소 및 모듈을 만드는 방법에 대한 자세한 자습서를 보려면이 사이트를 확인하십시오. 이 컨트롤러는 URL에보기 변수를 수정 해보기를 사용할 필요가 이러한 결정한다을 사용

https://docs.joomla.org/Developing_a_MVC_Component

+1

죽은 링크 ... 아마도 당신이 너무 다운 투표를 게시했을 때 아니 었어. – araisbec

+3

다른 곳에서 링크 할 때마다 링크 요약을 게시하십시오. –

관련 문제