PhpBB integration in Yii framework

Step-by-step instructions on how to quickly integrate the phpBB forum into your site written in the Yii framework


I have put together all the instructions for integrating the forum into the Yii framework, following which you are guaranteed to get a working forum and a small bonus at the end of the article.

It is assumed that you already have a valid site written in Yii, then you need to download and install the forum:
  1. Download the forum from the official phpBB website, if necessary, download the archive with crack in the same place
  2. Unzip the archive with the forum into the folder of your site, in the "/ forum " subdirectory , if you downloaded the crack, then also follow the steps that were described on the page from where the crack was downloaded (there is an instruction on which directories to put the files into Russification, note that you only need to supplement those directories with crack files, and not overwrite)
  3. Open yoursite.com/forum/index.php in the browser , you will see the forum installation page, perform the installation, there is nothing complicated in it. After the installation is complete, delete the folder " / forum / install " from your server .
    For the convenience of debugging the forum, I recommend uncommenting the lines in the " /forum/config.php " file :
    @define('DEBUG', true);
    @define('DEBUG_EXTRA', true);
    
    This will disable caching of forum templates and will display errors that occur.
  4. Download and install the extension for Yii (the original is here ), but I fixed it a bit.
    After downloading, unzip it to the directory " / protected / extensions / phpBB ", in the file " /protected/config/main.php " add the following:
    'components'=>array(
    	'phpBB'=>array(
    		'class'=>'ext.phpBB.phpBB',
    		'path'=>'webroot.forum',
    	),
    	'user'=>array(
    		'class'=>'PhpBBWebUser',
    		'loginUrl'=>array('/site/login'),
    		// enable cookie-based authentication
    		'allowAutoLogin'=>true,
    	),
    	'request'=>array(
    		// Возможно это и костыль, но без него никуда не поехать, тут мы определяем базовый URL нашего приложения.
    		'baseUrl'=>$_SERVER['DOCUMENT_ROOT'].$_SERVER['PHP_SELF'] != $_SERVER['SCRIPT_FILENAME'] ? 'http://'.$_SERVER['HTTP_HOST'] : '',
    		// ...
    	),
    	// ...
    ),
    
  5. Next, we do everything according to the instructions that have already been described here , but I will write the same thing here, only without unnecessary words.
    If the user class is already used on your Yii site , then do the following:
    • Open the file " /forum/includes/session.php " and on the 1500 line of code change the class name " user " to " bbuser " and rename the function inside the class:
      class user extends session
      {
          // ...    
          function user()    
          // ...
      }
      
      turn into:
      class bbuser extends session
      {
          // ...    
          function bbuser()    
          // ...
      }
      
    • In the file " /forum/common.php " on line 101 we change
      $user       = new user();
      
      on the
      $user       = new bbuser();
      

  6. In the directory " / protected / components " add a new file " PhpBBWebUser.php ", with the following contents:
    _identity = $identity;
    		return parent::login($identity, $duration);
    	}
    	protected function afterLogin($fromCookie) {
    		if ($this->_identity !== null) {
    			if (Yii::app()->phpBB->login($this->_identity->username, $this->_identity->password) != 'SUCCESS') {
    				Yii::log("Ошибка авторизации на форуме({$this->_identity->username})", CLogger::LEVEL_ERROR);
    			}
    		}
    		parent::afterLogin($fromCookie);
    	}
    	protected function afterLogout() {
    		Yii::app()->phpBB->logout();
    		parent::afterLogout();
    	}
    }
    
  7. Add the following lines to your user model (Yii):
    protected function afterSave() {
    	if ($this->isNewRecord) {
    		// Регистрация нового пользователя на форуме
    		// Логин, пароль(не захешированный), email, ID группы на форуме(по умолчанию 2-обычный пользователь, 5-администратор)
    		Yii::app()->phpBB->userAdd($this->login, $this->password, $this->email, 2);
    	}
    	parent::afterSave();
    }
    protected function afterDelete() {
    	// Удаляем пользователя с форума
    	Yii::app()->phpBB->userDelete($this->login);
    	parent::afterDelete();
    }
    

    If users can change passwords on your site, we add the following line to the password change action:
    Yii::app()->phpBB->changePassword($user_login, $user_new_password);
    
  8. If you need to close the registration and authorization on the forum, then in the file " /forum/ucp.php " we change the following lines with case:
    case 'register':
        header('location: /site/registration');
        exit();
    case 'login':
        header('location: /site/login');
        exit();
    case 'logout':
        header('location: /site/logout');
        exit();
    

On this part of the integration of the forum in your site comes to an end


Next, I will describe how easy it is to display the forum inside the template of your site without resorting to an iframe and without inventing a two-wheeled bicycle.
We will connect Yii directly to the forum:
  1. Create a new file " /forum/yiiapp.php " with the following contents:
    assetManager->setBasePath(Yii::getPathOfAlias('webroot').DS.'assets');
    
  2. In the file " /forum/index.php " (the main page of the forum), add the following lines to the very beginning:
    /**
     * Прикручиваем Yii к форуму
    */
    include "yiiapp.php";
    $controller = new Controller('bbforum'); // bbforum - произвольное название
    Yii::app()->controller = $controller;
    ob_start(); // Начинаем буферизацию вывода
    // ...
    

    Further in this file page_footer () is called at the end of the page, this is actually the output to the screen of the result of the work (in other forum files this function occurs several times in one file). This function displays the contents and finally makes exit (), we need to refine this function, open the file " /forum/includes/functions.php ", find the page_footer () function there and add it to the very end:
    function page_footer($run_cron = true)
    {
    	// ...
    	garbage_collection();
    	if (class_exists('Yii', false) && Yii::app()->controller !== null) {
    		$content = ob_get_clean();
    		Yii::app()->controller->renderPartial('//layouts/main', array('content'=>$content), false, true);
    	}
    	exit_handler();
    }
    


The most interesting line here is Yii :: app () -> controller-> renderPartial , we pass the whole result of the work of the forum script to Yii for processing, and now the main page of the forum will be displayed in the content part of your template // layouts / main !
You have to use this method for each forum page (for example, viewforum.php , viewtopic.php ).
Do not forget that after this we can use all the classes of your Yii-site inside the forum.

Of course, everything that is written above can be improved, but my task was to give you a working way ;-)

Thus, you can display absolutely any third-party script “as if” inside the general template of your Yii-project!

upd:A more current version of the article is here: http://ivan-orlov.com/en/articles/integraciya-phpbb-v-yii-framework

Also popular now: