Joomla源代碼解析(十五) 組件是如何被調用並渲染的

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決定進入那個分支。

再深入下去就是組件的整個生成過程,以後再看了。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章