Joomla - how to set up a component and link MVC together
I expect you understand MVC principle.
Joomla 1.5 developers eagerly praise MVC and there are uncounted articles written on Joomla MVC. And each one of them uses different approach. Joomla MVC framework should standardise arhitecture but unfortunately even core Joomla doesn't follow the same directory order. I don't know why this is but it is confusing and frustrating. It would be nice if all components followed simple rules and structure.
I read few articles, Joomla developer section and books. As far as I learned the proper way to organizes models, views and controllers in a component should be as follows:
| administrator/components | ||||||
| - com_examplecomponent |
root of our component should be prefixed with com_ |
|||||
| - controllers |
folder where all controllers should be located |
|||||
| - index.html - controller1.php - controller2.php |
controllers and empty index.html (for security only) |
|||||
| - models |
folder where all models should be located |
|||||
| - index.html - model1.php |
models and empty index.html (for security only) |
|||||
| - views |
views folder |
|||||
| - examplecomponent | for each view separate folder should be created default view is named the same as component |
|||||
| - tmpl | folder that holds layouts (HTML templates) to render the view |
|||||
| - index.html |
||||||
| - default.php |
||||||
| - index.html |
||||||
| - view.html.php |
view class stored in a file named view.documentType.php document types: feed, html, raw, pdf ... |
|||||
| - admin.examplecomponent.php |
PHP file named after the component and prefixed with admin this is the first file to be called when our component is accessed |
|||||
| - index.html |
||||||
administration base file code example
admin.examplecomponent.php is a first accessed file and can be looked at as controller of controllers.
<?php //get controller //get the controller require_once(JPATH_COMPONENT.DS.'controllers'.DS.$c.'.php'); $controller = new $controllerName(); $controller->execute('display'); $controller->redirect(); ?>
controller code example (how a view is linked inside controller)
<?php jimport('joomla.application.component.controller'); class ExamplecomponentControllerController1 extends JController { function display() { $document =& JFactory::getDocument(); $view = &$this->getView('product', $document->getType()); $view->setLayout('default'); $view->display(); } } ?>
Joomla component - view code example (view.html.php)
<?php jimport( 'joomla.application.component.view'); class SimpleorderformViewProduct extends JView { function display($tpl = null) { $greeting = "Hello World!"; $this->assignRef( 'greet', $greeting ); parent::display($tpl); } } ?>
Joomla component - view code example (view.html.php)
<!-- some html stuff --> <div> something here :))) <? // echo a variable set in a view ?> </div>
| < Prev | Next > |
|---|