Webeks.net - freelance programming
freelance programming - php, Joomla, Zend ...
Home

Joomla - how to set up a component and link MVC together

Written by Miha

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.

  1. <?php
  2. defined( '_JEXEC' ) or die( 'Restricted access' );
  3.  
  4. //get controller
  5. $c = strtolower( JRequest::getCmd('c', 'defaultcontroller'));
  6.  
  7. //get the controller
  8. require_once(JPATH_COMPONENT.DS.'controllers'.DS.$c.'.php');
  9.  
  10. $controllerName = ExamplecomponentController.ucfirst($c);
  11. $controller = new $controllerName();
  12. $controller->execute('display');
  13. $controller->redirect();
  14.  
  15. ?>

controller code example (how a view is linked inside controller)

  1. <?php
  2.  
  3. defined( '_JEXEC' ) or die( 'Restricted access' );
  4. jimport('joomla.application.component.controller');
  5.  
  6. class ExamplecomponentControllerController1 extends JController
  7. {
  8. function display()
  9. {
  10. $document =& JFactory::getDocument();
  11. $view = &$this->getView('product', $document->getType());
  12. $view->setLayout('default');
  13. $view->display();
  14. }
  15. }
  16.  
  17. ?>

Joomla component - view code example (view.html.php)

  1. <?php
  2.  
  3. defined( '_JEXEC' ) or die( 'Restricted access' );
  4. jimport( 'joomla.application.component.view');
  5.  
  6. class SimpleorderformViewProduct extends JView
  7. {
  8. function display($tpl = null)
  9. {
  10. $greeting = "Hello World!";
  11. $this->assignRef( 'greet', $greeting );
  12. parent::display($tpl);
  13. }
  14. }
  15. ?>

Joomla component - view code example (view.html.php)

  1. <!-- some html stuff -->
  2. <div>
  3. something here :)))
  4. <?
  5. // echo a variable set in a view
  6. echo $this->greet;
  7. ?>
  8. </div>
  9.  

 


blog comments powered by Disqus