Joomla components and execution path

The entry point into a Joomla! component is similar fromcomponent to component.  For this example, we willuse the Web Links core component.  The first fileto be executed in the front end is:

.../components/com_weblinks/weblinks.php

 

First we see the security check to make sure no one calls thispage directly.  This is standard and should be inall your php files (although there are a few exceptions):

// 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.  If so, we make sure to require therequired file in the controllers directory as well:

 

// 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.  Ifthe requested view is a weblink, we make sure to increment thecounter.  We then setup the view variable and callthe diplay method on our parent class (JController).

 

One thing to notice here is the getModel call. This will go out and load up the required modelfor the component.  In this case, it will load theweblink model found here:

.../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. A lot of the logic here is specific to the WebLinks component, but you will start to see similar functionalitybeing used across a lot of component view classes. At the end of the display method, this class willcall the parent (JView) display method passing in the template nameto display.  If no name is passed in, the"default" template is used.

 

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.  In this case,the next file to look at is this one:

.../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>
                &nbsp;
                <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.


A few of the other file types you might find incomponents:
  • 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


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值