The entry point into a Joomla! component is similar fromcomponent to component. <wbr>For this example, we willuse the Web Links core component. <wbr>The first fileto be executed in the front end is:</wbr></wbr>
.../components/com_weblinks/weblinks.php
<wbr></wbr>
First we see the security check to make sure no one calls thispage directly. <wbr>This is standard and should be inall your php files (although there are a few exceptions):</wbr>
// no direct access
defined('_JEXEC') or die('Restricted access');
<wbr></wbr>
Next, we require (include) the controller.php class that is inthe same directory:
// Require the base controller
require_once (JPATH_COMPONENT.DS.'controller.php');
<wbr></wbr>
We check the query string to see if a specific controller namewas sent in. <wbr>If so, we make sure to require therequired file in the controllers directory as well:</wbr>
<wbr></wbr>
// 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 = '';
}
}
<wbr></wbr>
<wbr></wbr>
We then create an instance of our controller using the name weset above:
// Create the controller
$classname = 'WeblinksController'.ucfirst($controller);
$controller = new $classname( );
<wbr></wbr>
And execute the task that was requested in the query stringparameter:
// Perform the Request task
$controller->execute(JRequest::getCmd('task'));
<wbr></wbr>
Once the task is complete, we redirect if needed:
// Redirect if set by the controller
$controller->redirect();
<wbr></wbr>
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.
<wbr></wbr>
<?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);
}
}
}
<wbr></wbr>
<wbr></wbr>
In this method, we set the default view to be categories if onewas not passed in as a query string parameter. <wbr>Ifthe requested view is a weblink, we make sure to increment thecounter. <wbr>We then setup the view variable and callthe diplay method on our parent class (JController).</wbr></wbr>
<wbr></wbr>
One thing to notice here is the getModel call.<wbr>This will go out and load up the required modelfor the component. <wbr>In this case, it will load theweblink model found here:</wbr></wbr>
.../components/com_weblinks/models/weblink.php
<wbr></wbr>
At this point, we will assume no view was passed in andtherefore our view will be set to "categories".
<wbr></wbr>
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);
}
}
?>
<wbr></wbr>
Again, it is a very simple class with one display method.<wbr>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.<wbr>At the end of the display method, this class willcall the parent (JView) display method passing in the template nameto display. <wbr>If no name is passed in, the"default" template is used.</wbr></wbr></wbr>
<wbr></wbr>
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. <wbr>In this case,the next file to look at is this one:</wbr>
.../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. <wbr>These files typically just storecommon functionality for the component.</wbr>
- 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.<wbr>These are used when creating menu items for acomponent.</wbr>
- index.html - It's good practice to have a blank index.html filein all your directories. <wbr>This is another securitymeasure.</wbr>
- css/images/js - Folders that contain different files forstyling and client side functionality
本文介绍了 Joomla! CMS 中组件的工作流程,从入口文件到视图展示的过程,包括安全性检查、控制器任务执行、模型加载及视图缓存逻辑等关键步骤。
1096

被折叠的 条评论
为什么被折叠?



