phpBB3 integration in Yii framework

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

I gathered together all the instructions for integration forum in Yii framework, by doing that, you are guaranteed to get a working forum and a little bonus at the end.
  1. Download the forum from official site phpBB.
  2. Unpack archive with the forum in a folder of your site in a subdirectory "/forum".
  3. Open in your browser yoursite.com/forum/index.php, you will install page, perform the installation, it is no big deal. Upon completion of installation, remove the folder from your server "/forum/install".
    For ease of debugging forum I recommend uncommented in the file "/forum/config.php" line:
    1. @define('DEBUG', true);
    2. @define('DEBUG_EXTRA', true);
    This will disable templates caching and will display errors encountered.
  4. Download the extension for Yii and install it.
    Once downloaded, unpack it into the directory " /protected/extensions/phpBB", in the file "/protected/config/main.php" add the following:
    1. <?php
    2.  
    3. 'components'=>array(
    4. 'phpBB'=>array(
    5. 'class'=>'ext.phpBB.phpBB',
    6. 'path'=>'webroot.forum',
    7. ),
    8. 'user'=>array(
    9. 'class'=>'PhpBBWebUser',
    10. 'loginUrl'=>array('/site/login'),
    11. // enable cookie-based authentication
    12. 'allowAutoLogin'=>true,
    13. ),
    14. 'request'=>array(
    15. // we define the base URL of the application, it is necessary.
    16. 'baseUrl'=>$_SERVER['DOCUMENT_ROOT'].$_SERVER['PHP_SELF'] != $_SERVER['SCRIPT_FILENAME'] ? 'http://'.$_SERVER['HTTP_HOST'] : '',
    17. // ...
    18. ),
    19. // ...
    20. ),
  5. If your Yii-app already have the class "User", then run the following:
    • Open the file "/forum/includes/session.php" and 1500 line of code change the class name "user" on "bbuser" and rename the function inside the class:
      1. class user extends session
      2. {
      3. // ...
      4. function user()
      5. // ...
      6. }
      convert to:
      1. class bbuser extends session
      2. {
      3. // ...
      4. function bbuser()
      5. // ...
      6. }
    • In the file "/forum/common.php" on line 101 change:
      1. $user = new user();
      to:
      1. $user = new bbuser();
  6. In the directory "/protected/components" add new file "PhpBBWebUser.php", with the following contents:
    1. class PhpBBWebUser extends WebUser{
    2. /** @var UserIdentity */
    3. private $_identity;
    4. public function login($identity, $duration=0) {
    5. $this->_identity = $identity;
    6. return parent::login($identity, $duration);
    7. }
    8. protected function afterLogin($fromCookie) {
    9. if ($this->_identity !== null) {
    10. if (Yii::app()->phpBB->login($this->_identity->username, $this->_identity->password) != 'SUCCESS') {
    11. Yii::log("Error logging into your account({$this->_identity->username})", CLogger::LEVEL_ERROR);
    12. }
    13. }
    14. parent::afterLogin($fromCookie);
    15. }
    16. protected function afterLogout() {
    17. Yii::app()->phpBB->logout();
    18. parent::afterLogout();
    19. }
    20. }
  7. In your users model (Yii) add the following lines:
    1. protected function afterSave() {
    2. if ($this->isNewRecord) {
    3. // Register a new forum user
    4. // User name, password(only original, non hash), email, ID group forum (default is 2-normal user, 5-administrator)
    5. Yii::app()->phpBB->userAdd($this->login, $this->password, $this->email, 2);
    6. }
    7. parent::afterSave();
    8. }
    9. protected function afterDelete() {
    10. // Remove the user from the forum
    11. Yii::app()->phpBB->userDelete($this->login);
    12. parent::afterDelete();
    13. }
    If your site users can change their passwords, then add following line to the function(your) of changing the password:
    1. Yii::app()->phpBB->changePassword($user_login, $user_new_password);
  8. If you need to cancel the registration and authorization on the forum, the file "/forum/ucp.php" change the following lines:
    1. case 'register':
    2. header('location: /site/registration');
    3. exit();
    4. case 'login':
    5. header('location: /site/login');
    6. exit();
    7. case 'logout':
    8. header('location: /site/logout');
    9. exit();

In this part of the forum integration into your site is nearing completion

Next, I will describe how to simply display a forum within the template of your site without using iframe.
We will connect Yii directly in the forum:
  1. Create a new file "/forum/yiiapp.php" with the following contents:
    1. <?php
    2. // Yii-application for forum
    3. defined('DS') or define('DS', DIRECTORY_SEPARATOR);
    4. // change the following paths if necessary
    5. $yii = dirname(__FILE__).DS.'..'.DS.'yii'.DS.'framework'.DS.'yii.php';
    6. $yii_config = dirname(__FILE__).DS.'..'.DS.'protected'.DS.'config'.DS.'main.php';
    7. require_once($yii);
    8. Yii::createWebApplication($yii_config);
    9. // Override the webroot directory, from "/server/www/forum" to "/server/www", otherwise Yii will not work as we need
    10. Yii::setPathOfAlias('webroot', Yii::getPathOfAlias('webroot').DS.'..');
    11. // Do the same for assets folder
    12. Yii::app()->assetManager->setBasePath(Yii::getPathOfAlias('webroot').DS.'assets');
  2. In the file "/forum/index.php" (forum homepage) in the beginning add the following lines:
    1. /**
    2.  * Screw Yii to the forum
    3. */
    4. include "yiiapp.php";
    5. $controller = new Controller('bbforum'); // bbforum - an arbitrary name
    6. Yii::app()->controller = $controller;
    7. ob_start(); // Start buffering the output
    8. // ...
    Later in this file at the end of the page is called page_footer(), it is the function for render phpBB forum content(in other forum files this function occurs more than once in a single file). This function displays the contents and in the end makes the exit(), we need to modify this function, open the file " /forum/includes/functions.php", we find there function page_footer() and modify at the end of it:
    1. function page_footer($run_cron = true)
    2. {
    3. // ...
    4. garbage_collection();
    5. if (class_exists('Yii', false) && Yii::app()->controller !== null) {
    6. $content = ob_get_clean();
    7. Yii::app()->controller->renderPartial('//layouts/main', array('content'=>$content), false, true);
    8. }
    9. exit_handler();
    10. }
The most interesting line here
  1. Yii::app()->controller->renderPartial()
we are all forums contents pass to handle into Yii, and now the main forum page will be displayed in the content part of your template " //layouts/main"! This way you have to use for each page in the forum (eg viewforum.php, viewtopic.php).
Do not forget that after this we can use all of your classes from Yii within the forum.

Of course all that is written above, you can improve, but my goal was to give you a working method ;-)

So you can use any third party script to display "as if" within the overall layout of your Yii-project!

Comments (0)


Leave a comment