📜 ⬆️ ⬇️

PhpBB integration in the Yii framework

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


I have put together all the instructions for integrating the forum in the Yii framework, by 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 site of phpBB, if necessary, download the archive with the cracker in the same place
  2. Unzip the archive with the forum in the folder of your site, in the subdirectory "/ forum ", if you downloaded the crack, then just follow the steps that were described on the page where you downloaded the crack (there is an instruction on which directories you need to cram files Russification, note that you only need to supplement those directories with the files of the cracker, and not to 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, remove 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 the 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.
    Once downloaded, unzip it into 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. Then we do everything according to the instructions that have already been described here , but I will write the same thing here, only without further ado.
    If you already use the " user " class in your Yii site, do the following:
    • Open the file " /forum/includes/session.php " and on the 1500 line of code change the name of the class " 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
       $user = new bbuser(); 

  6. In the directory " / protected / components " add a new file " PhpBBWebUser.php ", with the following contents:
     <?php class PhpBBWebUser extends WebUser{ /** @var UserIdentity */ private $_identity; public function login($identity, $duration=0) { $this->_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. We 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, then 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 `s:
     case 'register': header('location: /site/registration'); exit(); case 'login': header('location: /site/login'); exit(); case 'logout': header('location: /site/logout'); exit(); 

This part of the integration of the forum into your site is coming to an end.


Next, I will describe how easy it is to display a forum inside a template of your site without resorting to an iframe and not 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:
     <?php // Yii-   defined('DS') or define('DS', DIRECTORY_SEPARATOR); // change the following paths if necessary $yii = dirname(__FILE__).DS.'..'.DS.'yii'.DS.'framework'.DS.'yii.php'; $yii_config = dirname(__FILE__).DS.'..'.DS.'protected'.DS.'config'.DS.'main.php'; require_once($yii); Yii::createWebApplication($yii_config); //     "/server/www/forum"  "/server/www",  Yii       Yii::setPathOfAlias('webroot', Yii::getPathOfAlias('webroot').DS.'..'); //       assets Yii::app()->assetManager->setBasePath(Yii::getPathOfAlias('webroot').DS.'assets'); 
  2. In the file " /forum/index.php " (the main forum page) we 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 at the end of the page page_footer () is called, this is actually the output of the work to the screen (in other forum files this function occurs several times in one file). This function displays the content and finally does exit (), we need to refine this function, open the file " /forum/includes/functions.php ", find the page_footer () function 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 transfer all the results of the forum script to Yii for processing, and now the main forum page will be displayed in the content part of your template " // layouts / main "!
You will 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 absolutely any third-party script display "as if" inside the overall template of your Yii project!
')
upd: a more current version of the article here: http://ivan-orlov.com/ru/articles/integraciya-phpbb-v-yii-framework

Source: https://habr.com/ru/post/190868/


All Articles