免费打码平台:joomla ? joomla mvc

来源:百度文库 编辑:中财网 时间:2024/04/28 12:33:50

MVC组件的执行

时间:2011/05/08 11:21 am  作者:冰冻解封 |  28次阅读 | 评论:0

以前的文章中,我们曾经说过 $mainframework->dispatch 是如何最终调用组件的,通过这个dispatch,最终 include 相应组件目录下的 组件名称.php 文件,现在我们来看看,这个文件是怎么按部就班的联系了MVC模式相关的各个文件。

require_once (JPATH_COMPONENT.DS.'controller.php');

// Require specific controller if requested
if($controller = JRequest::getVar('controller')) {
require_once (JPATH_COMPONENT.DS.'controllers'.DS.$controller.'.php');
}

// Create the controller
$classname = 'HelloController'.$controller;
$controller = new $classname( );

// Perform the Request task
$controller->execute( JRequest::getVar('task'));

// Redirect if set by the controller
$controller->redirect();

其实就是根据request提交的controller参数,创建相应的JController对象,然后由controoler对象执行相应的任务。

这样我们就完全理解了,一个组件是如何被调用,MVC组件是如何执行,并最后返回html代码的。

模块是如何被调用执行并渲染?
文章分类:PHP编程
以前的文章中,关于/index.php我们已经分析完了 $mainframe->dispatch()是引入了组件,并被执行。我们知道对于Joomla,一个页面只能有一个或者0个组件,而上,下左右的碎片都是module,module是页面丰富的有效补充。比如我们知道菜单是  mod_mainmenu,而footer是mod_footer等等,那么这些module是怎么被引入的,并最后执行的?

秘密都在$mainframe->render()这个函数上,我们看看这个函数都做了什么工作。

以下是JSite 的render 函数的内容

  $document =& JFactory::getDocument();
  $user     =& JFactory::getUser();

  // get the format to render
  $format = $document->getType();

  switch($format)
  {
   case 'feed' :
   {
    $params = array();
   } break;

   case 'html' :
   default     :
   {
    $template = $this->getTemplate();
    $file   = JRequest::getCmd('tmpl', 'index');

    if ($this->getCfg('offline') && $user->get('gid') < '23' ) {
     $file = 'offline';
    }
    if (!is_dir( JPATH_THEMES.DS.$template ) && !$this->getCfg('offline')) {
     $file = 'component';
    }
    $params = array(
     'template'  => $template,
     'file'  => $file.'.php',
     'directory' => JPATH_THEMES
    );
   } break;
   }

  $data = $document->render( $this->getCfg('caching'), $params);
  JResponse::setBody($data);

其实重要的部分是引入了相应的模板文件(template/***/index.php),并调用了 JDocumentHtml的 render 函数。

看到这里,我们终于明白了,模板的index.php原来是这个时候被引入的。

我们再看看 JDocumentHtml 的render函数。

这个函数中最重要的两句程序是

  $data = $this->_loadTemplate($directory.DS.$template, $file); 载入模板文件
  $data = $this->_parseTemplate($data); 解析模板

再继续看看解析模板是什么过程:

  $replace = array();
  $matches = array();
  if(preg_match_all('##iU', $data, $matches))
  {
   $matches[0] = array_reverse($matches[0]);
   $matches[1] = array_reverse($matches[1]);
   $matches[2] = array_reverse($matches[2]);

   $count = count($matches[1]);

   for($i = 0; $i < $count; $i++)
   {
    $attribs = JUtility::parseAttributes( $matches[2][$i] );
    $type  = $matches[1][$i];

    $name  = isset($attribs['name']) ? $attribs['name'] : null;
    $replace[$i] = $this->getBuffer($type, $name, $attribs);
   }

   $data = str_replace($matches[0], $replace, $data);
  }

  return $data;
}

对了,就是这部分,对模板中 JDOC标签进行了解析,获得了相应的module名称和参数,并调用getBuffer函数执行。

至此 调用 $renderer->render($name, $attribs, $result);