2011-03-16 3 views
12

Je suis nouveau sur Joomla, je veux savoir comment le contrôleur Joomla transmet les données au modèle, au contrôleur et au contrôleur à afficher. Bien que cela puisse être une question stupide, j'ai vraiment essayé de trouver la réponse. J'espère que je peux obtenir de l'aide de la famille stackoverflow.Comment fonctionne Joomla Model View Controller (MVC)?

+0

BTW MVC est l'abréviation de Model View Controller – Martin

Répondre

29

Le contrôleur prend la variable de vue dans l'URL et l'utilisation de ces qui détermine point de vue doit être utilisé. Il définit ensuite la vue à utiliser. La vue appelle ensuite le modèle pour récupérer les données dont il a besoin et les transmet ensuite au tmpl pour être affiché.

Voici une configuration simple de la façon dont tout cela fonctionne ensemble:

composants/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); 
    } 

} 

composants/com_test/vues/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(); 
    } 

} 

composants/com_test/modèles/uneVue.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(); 
    } 

} 

composants/com_test/vues/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');', d'où vient-il 'some_var'? Est-ce à partir de l'URL encodée? – Plummer

+1

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

+0

Donc. Dans Joomla est la vue qui interagit avec le modèle pour obtenir les données? – Nobita

2

consultez ce site pour obtenir un tutoriel détaillé sur la façon de créer des composants et des modules à l'aide du MVC de Joomla. Hope it helps

https://docs.joomla.org/Developing_a_MVC_Component

+1

Dead link ... ne l'était probablement pas lorsque vous avez posté donc pas de vote négatif. – araisbec

+3

Chaque fois que vous liez un lien ailleurs, veuillez également publier un récapitulatif du lien. –

Questions connexes