Yii2 Simple Chat ¶
A simple chat for your yii2 application
Installation ¶
The preferred way to install this extension is through composer.
Either run
php composer.phar require --prefer-dist bubasuma/yii2-simplechat
or add
"bubasuma/yii2-simplechat": "~2.0.0"
to the require section of your composer.json file.
Demo ¶
Once the extension is installed, simply modify your application configuration as follows:
return [
'bootstrap' => ['simplechat'],
'modules' => [
'simplechat' => [
'class' => 'bubasuma\simplechat\Module',
],
// ...
],
// ...
];
Use the same configuration for your console application:
>Note: You need this configuration to access simple chat via command line. You can remove it in production mode.
You can access Simple Chat via command line as follows:
# change path to your application's base path
cd path/to/AppBasePath
# show available commands
php yii simplechat
# create test tables, generates and load fixtures
php yii simplechat/start
# unload fixtures
php yii simplechat/clean
# unload fixtures and load them again
php yii simplechat/reset
# unload fixtures and drop test tables
php yii simplechat/stop
You can specify different options of the start
and reset
command:
# You can specify how many fixtures per user and message you need by the --users and --messages options
php yii simplechat/start --users=50 --messages=10000
php yii simplechat/reset --users=20 --messages=5000
# You can specify in what language to generate fixtures by the --language option. Thanks to yii2-faker
php yii simplechat/start --language="ru_RU"
php yii simplechat/reset --language="fr_FR"
You can then access Simple Chat through the following URL:
http://localhost/path/to/index.php?r=messages
or if you have enabled pretty URLs, you may use the following URL:
http://localhost/path/to/index.php/messages
You should see the below:
If not, please check if demo migration has been successfully applied against your database. You can check it by running the following command:
yii simplechat/start
>Note: the command above is accessible only if you have configured your console application as it is recommended above.
Usage ¶
Extend the main conversation
class like follow:
namespace common\models;
use common\models\User;
//...
class Conversation extends \bubasuma\simplechat\db\Conversation
{
public function getContact()
{
return $this->hasOne(User::className(), ['id' => 'contact_id']);
}
/**
* @inheritDoc
*/
protected static function baseQuery($userId)
{
return parent::baseQuery($userId) ->with(['contact.profile']);
}
/**
* @inheritDoc
*/
public function fields()
{
return [
//...
'contact' => function ($model) {
return $model['contact'];
},
'deleteUrl',
'readUrl',
'unreadUrl',
//...
];
}
}
Extend the main message
class like follow:
namespace common\models;
//...
class Message extends \bubasuma\simplechat\db\Message
{
/**
* @inheritDoc
*/
public function fields()
{
return [
//...
'text',
'date' => 'created_at',
//...
];
}
}
Create a controller like follow:
namespace frontend\controllers;
//...
use yii\web\Controller;
use common\models\Conversation;
use common\models\Message;
use bubasuma\simplechat\controllers\ControllerTrait;
//...
class MessageController extends Controller
{
use ControllerTrait;
/**
* @return string
*/
public function getMessageClass()
{
return Message::className();
}
/**
* @return string
*/
public function getConversationClass()
{
return Conversation::className();
}
}
>Note: If you are using this extension in your frontend application, you can find the usage of widgets in index.twig.
FAQ ¶
Does this extension work with any template engines other than twig
?
Yes. Given that, the default render in yii2
is php
, you must indicate explicitly the extension part in view names.
Can I use this extension in a RESTful APIs?
Yes, You can.
Can I use different template engines for rendering in server side and client side?
Yes. But using the same template in both sides remains the best implementation.
The extension is great but I have a difficulty
I simply want all my users to be able to chat with each other.
I have follow all the tutorial and everything is working well.
I have added the controller and model classes as required but I do not know how to display the view as shown above.
Please how do I display the chat view to the User ??
thanks
Hello! I installed yii2-simplechat on yii2 using the instructions: https://github.com/bubasuma/yii2-simplechat. He did everything right. Here is the chat page: http://premium-my-freestyle.esy.es/messages. There, of course, DEMO. I started php yii simplechat start. How do I make the messages go? I write a message, I press the "Send Message" button, but no messages are sent. What do i do? I have remodeled some classes and created a MessageController as written there. The fact that I have a "native" table to this site is the USERS table, but simplechat creates its own (SIMPLECHAT_USERS -where so), to its standards. But it does not matter. What do i do? It is necessary that messages are sent and the number of new messages received from each user is highlighted. Help me please! How should I proceed?
20188
I investigated why after you press "Send Message" button, the message is not sent. In fact the textarea is not in a form, although it's supposed be. see https://github.com/bubasuma/yii2-simplechat/blob/master/views/default/form.twig
not able to submit message,Please help.
Hello! I installed yii2-simplechat on yii2 using the instructions: https://github.com/bubasuma/yii2-simplechat. He did everything right. Here is the chat page: http://premium-my-freestyle.esy.es/messages. There, of course, DEMO. I started php yii simplechat start. How do I make the messages go? I write a message, I press the "Send Message" button, but no messages are sent. What do i do? I have remodeled some classes and created a MessageController as written there. The fact that I have a "native" table to this site is the USERS table, but simplechat creates its own (SIMPLECHAT_USERS -where so), to its standards. But it does not matter. What do i do? It is necessary that messages are sent and the number of new messages received from each user is highlighted. Help me please! How should I proceed?
I simply want all my users to be able to chat with each other.
I have follow all the tutorial and everything is working well.
I have added the controller and model classes as required but I do not know how to display the view as shown above.
Please how do I display the chat view to the User ??
thanks
If you have any questions, please ask in the forum instead.
Signup or Login in order to comment.