2rats/yii2-forum

Installs: 55

Dependents: 0

Suggesters: 0

Security: 0

Stars: 1

Watchers: 0

Forks: 0

Open Issues: 0

Type:yii2-extension

dev-main 2024-03-16 17:03 UTC

This package is auto-updated.

Last update: 2024-04-16 17:09:16 UTC


README

This Yii2 extension offers a simple set of forum features that make it easy to integrate a full-featured forum into your web application.

⚠️ This extension is currently WIP. Feel free to Watch this repo for the first release or contribute. Thank you. ⚠️

Requirements

  • PHP 7.4 or higher
  • Yii2 Framework
  • Yii2 RBAC
  • User table migration

Instalation

This extension is designed to be used out of the box. Once you have installed the necessary migrations and configured the module in your application structure, you'll be ready to go.

The preferred way to install this extension is through composer.

composer require 2rats/yii2-forum "@dev"

Apply database migrations

1. User table and RBAC

First of all you have to setup and apply your User table and RBACK migration using:

php yii migrate
php yii migrate --migrationPath=@yii/rbac/migrations

2. Extension

Then you have to to apply migrations of this extension:

php yii migrate --migrationPath=@rats/forum/migrations
php yii migrate --migrationPath=@rats/forum/migrations/rbac

Modify database connection

This extension parses text emoji to unicode symbols (for example ':D' => '😃'). For proper saving to the database, it is necessary to have the correct charset both in the database settings and in the connection to the database in the config/db.php configuration file.

For example, you can use utf8mb4 charset.

<?php

return [
    'class' => 'yii\db\Connection',
    'dsn' => 'mysql:host=localhost;dbname=test_db_name',
    'username' => 'root',
    'password' => '',
    'charset' => 'utf8mb4',
];

Modify signup method

  1. Open your application's signup process, where you create new users. This could be within a controller action, signup form or service that handles user registration.

  2. Inside your signup process, after creating the main user instance, call the ForumModule::signupUser function to create the associated forum user:

use rats\forum\ForumModule;

// ... Your signup process code ...

// After creating the main user, call the signupUser function to create the forum user.
ForumModule::signupUser($user);

Here, $user should be an instance of yours app\models\User.

If you are using the default Yii2 signup form, you can change it like this:

use rats\forum\ForumModule;

class SignupForm extends Model{

	// ... other code ...
	
	public function signup()
	{
	    if (!$this->validate()) {
	        return null;
	    }
	    $user = new User();
	    $user->username = $this->username;
	    $user->email = $this->email;
	    $user->setPassword($this->password);
	    $user->generateAuthKey();
	    $user->generateEmailVerificationToken();
	    
	    return $user->save() && $this->sendEmail($user) && ForumModule::signupUser($user);
	}
	
	// ... other code ...

}

Configuration

Module configuration

Configuration options of the module in config/web.php.

option default value description
userClass 'app\models\User' Class that represents app User.
The class must extend \yii\db\ActiveRecord
forumLayout 'forum' Layout that is used in forum sites
adminLayout 'admin' Layout that is used in administration of the forum

Parameters configuration

Module behavior can also be modified by adding parameters in your configuration file config/params.php.

option default value description
migrationTableOptions 'CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci ENGINE=InnoDB' | null $options for createTable() function.
By default, it sets charset and engine for the mysql database driver, null for the others.
userTableName 'user' Table name of the Class, that represents app User.

Contributions

Contributions are highly appreciated. If you would like to contribute, please fork the repository, make your changes, and submit a pull request or open an issue.

Authors

License

BSD 3-Clause License