The entry point into a Joomla! component is similar fromcomponent to component.
.../components/com_weblinks/weblinks.php
First we see the security check to make sure no one calls thispage directly.
// no direct access
defined('_JEXEC') or die('Restricted access');
Next, we require (include) the controller.php class that is inthe same directory:
// Require the base controller
require_once (JPATH_COMPONENT.DS.'controller.php');
We check the query string to see if a specific controller namewas sent in.
// Require specific controller if requested
if($controller = JRequest::getWord('controller')) {
$path = JPATH_COMPONENT.DS.'controllers'.DS.$controller.'.php';
if (file_exists($path)) {
require_once $path;
} else {
$controller = '';
}
}
We then create an instance of our controller using the name weset above:
// Create the controller
$classname = 'WeblinksController'.ucfirst($controller);
$controller = new $classname( );
And execute the task that was requested in the query stringparameter:
// Perform the Request task
$controller->execute(JRequest::getCmd('task'));
Once the task is complete, we redirect if needed:
// Redirect if set by the controller
$controller->redirect();
Next, we open up the controller class.
The generic controller for the Web Links component can be foundhere:
.../components/com_weblinks/controller.php
All this class defines is the display method, which is thedefault used if the user does not specify a task.
<?php // Check to ensure this file is included in Joomla! defined('_JEXEC') or die( 'Restricted access' ); jimport('joomla.application.component.controller'); class WeblinksController extends JController { function display() { // Set a default view if none exists if ( ! JRequest::getCmd( 'view' ) ) { JRequest::setVar('view', 'categories' ); } //update the hit count for the weblink if(JRequest::getCmd('view') == 'weblink') { $model =& $this->getModel('weblink'); $model->hit(); } // View caching logic -- simple... are we logged in? $user = &JFactory::getUser(); $view = JRequest::getVar('view'); $viewcache = JRequest::getVar('viewcache', '1', 'POST', 'INT'); if ($user->get('id') || ($view == 'category' && $viewcache == 0)) { parent::display(false); } else { parent::display(true); } } }
In this method, we set the default view to be categories if onewas not passed in as a query string parameter.
One thing to notice here is the getModel call.
.../components/com_weblinks/models/weblink.php
At this point, we will assume no view was passed in andtherefore our view will be set to "categories".
Next, we open up the view class.
Since we are assuming we want the categories view, this is thenext file to be executed:
.../components/com_weblinks/views/categories/view.html.php
<?php // Check to ensure this file is included in Joomla! defined( '_JEXEC' ) or die( 'Restricted access' ); jimport( 'joomla.application.component.view'); class WeblinksViewCategories extends JView { function display( $tpl = null) { global $mainframe; $document =& JFactory::getDocument(); $categories =& $this->get('data'); $total =& $this->get('total'); $state =& $this->get('state'); // Get the page/component configuration $params = &$mainframe->getParams(); $menus = &JSite::getMenu(); $menu = $menus->getActive(); // because the application sets a default page title, we need to get it // right from the menu item itself if (is_object( $menu )) { $menu_params = new JParameter( $menu->params ); if (!$menu_params->get( 'page_title')) { $params->set('page_title', JText::_( 'Web Links' )); } } else { $params->set('page_title', JText::_( 'Web Links' )); } $document->setTitle( $params->get( 'page_title' ) ); // Set some defaults if not set for params $params->def('comp_description', JText::_('WEBLINKS_DESC')); // Define image tag attributes if ($params->get('image') != -1) { if($params->get('image_align')!="") $attribs['align'] = $params->get('image_align'); else $attribs['align'] = ''; $attribs['hspace'] = 6; // Use the static HTML library to build the image tag $image = JHTML::_('image', 'images/stories/'.$params->get('image'), JText::_('Web Links'), $attribs); } for($i = 0; $i < count($categories); $i++) { $category =& $categories[$i]; $category->link = JRoute::_('index.php?option=com_weblinks&view=category&id='. $category->slug); // Prepare category description $category->description = JHTML::_('content.prepare', $category->description); } $this->assignRef('image', $image); $this->assignRef('params', $params); $this->assignRef('categories', $categories); parent::display($tpl); } } ?>
Again, it is a very simple class with one display method.
And last, we open up the template class.
We will assume that no template name was passed in and thereforethe default template is being used.
.../components/com_weblinks/views/categories/tmpl/default.php
<?php // no direct access defined('_JEXEC') or die('Restricted access'); ?> <?php if ( $this->params->def( 'show_page_title', 1 ) ) : ?> <div class="componentheading"> <?php echo $this->escape($this->params->get('page_title')); ?> </div> <?php endif; ?> <?php if ( ($this->params->def('image', -1) != -1) || $this->params->def('show_comp_description', 1) ) : ?> <table width="100%" cellpadding="4" cellspacing="0" border="0" align="center" class="contentpane"> <tr> <td valign="top" class="contentdescription"> <?php if ( isset($this->image) ) : echo $this->image; endif; echo $this->params->get('comp_description'); ?> </td> </tr> </table> <?php endif; ?> <ul> <?php foreach ( $this->categories as $category ) : ?> <li> <a href="/" class="category"> <?php echo $this->escape($category->title);?></a> <span class="small"> (<?php echo $category->numlinks;?>) </span> </li> <?php endforeach; ?> </ul>
Most of the logic here will be very specific to the component being executed.
- Helpers - There may be a helper.php file or a helpers directorywith many files.
These files typically just storecommon functionality for the component. - Assets - This seems to be a catch-all folder for other thingsto include in a component.
- router.php - This file is used when SEF urls are turned on totranslate URLs in both directions.
- xml files - These typically define parameters and otherinformation about the component and it's views.
These are used when creating menu items for acomponent. - index.html - It's good practice to have a blank index.html filein all your directories.
This is another securitymeasure. - css/images/js - Folders that contain different files forstyling and client side functionality