Joomla源代码解析(十五) 组件是如何被调用并渲染的

来源:互联网 发布:ubuntu 16.04挂载硬盘 编辑:程序博客网 时间:2024/06/08 03:44

Joomla代码中, 组件是如何被调用并渲染的呢?

在描述 /index.php的时候,我们看到根据option参数,$mainframework->dispatch(),就进入了组件的调用并渲染的过程,我们来看看JSite 的dispatch都做了什么工作。

dispatch 最关键的是这几句话:

  $document->setTitle( $params->get('page_title') );   //设置标题
  $document->setDescription( $params->get('page_description') );  //设置meta

  $contents = JComponentHelper::renderComponent($component);
  $document->setBuffer( $contents, 'component');

可以看到最为关键的是 JComponentHelper::renderComponent($component);

再看看这一行程序完成了那些工作

  $task = JRequest::getString( 'task' );

  // Build the component path
  $name = preg_replace('/[^A-Z0-9_\.-]/i', ', $name);
  $file = substr( $name, 4 );

  // Define component path
  define( 'JPATH_COMPONENT',     JPATH_BASE.DS.'components'.DS.$name);
  define( 'JPATH_COMPONENT_SITE',    JPATH_SITE.DS.'components'.DS.$name);
  define( 'JPATH_COMPONENT_ADMINISTRATOR', JPATH_ADMINISTRATOR.DS.'components'.DS.$name);

  // get component path
  if ( $mainframe->isAdmin() && file_exists(JPATH_COMPONENT.DS.'admin.'.$file.'.php') ) {
   $path = JPATH_COMPONENT.DS.'admin.'.$file.'.php';
  } else {
   $path = JPATH_COMPONENT.DS.$file.'.php';
  }

这部分实际上确定了那个compoent下的组件文件被引入,并取得了task,中间一部分兼容代码就不看了

我们来看关键代码:

  ob_start();
  require_once $path;
  $contents = ob_get_contents();
  ob_end_clean();

这部分代码就是包含了组件的开始文件,而这个文件,我们在组件开发的时候用到的。这个文件引入了controller 文件,并根据task决定进入那个分支。

再深入下去就是组件的整个生成过程,以后再看了。

原创粉丝点击