2011-03-16 25 views
12

Soy nuevo en Joomla, quiero saber cómo el controlador Joomla transmite datos al modelo, modelo a controlador y controlador para ver. Aunque esta podría ser una pregunta tonta, realmente traté de encontrar la respuesta. Espero que pueda obtener ayuda de la familia stackoverflow.¿Cómo funciona Joomla Model View Controller (MVC)?

+0

BTW MVC significa Controlador de vista de modelo – Martin

Respuesta

29

El controlador recoge la variable de vista en la url y el uso de estos determina qué visión tiene que ser utilizado. Luego establece la vista para ser utilizada. Luego, la vista llama al modelo para obtener los datos que requiere y luego pasa esto a la tmpl para mostrarse.

abajo es una preparación simple de cómo funciona todo esto en conjunto:

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

} 

componentes/com_test/views/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(); 
    } 

} 

componentes/com_test/modelos/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(); 
    } 

} 

componentes/com_test/views/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');', ¿de dónde sale 'some_var'? ¿Es de la URL codificada? – Plummer

+1

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

+0

So. En Joomla, ¿es la vista que interactúa con el modelo para obtener los datos? – Nobita

2

echa un vistazo a este sitio para obtener un tutorial detallado sobre cómo hacer componentes y módulos utilizando el MVC de Joomla. Espero que ayuda

https://docs.joomla.org/Developing_a_MVC_Component

+1

Enlace muerto ... probablemente no fue cuando publicaste así que no voto abajo. – araisbec

+3

Cada vez que se enlace a otro sitio, publique también una recapitulación del enlace. –

Cuestiones relacionadas