gofmanaa / yii2-simplechat
为您的yii2应用程序提供的简单聊天
v2.0.1
2016-05-31 08:22 UTC
Requires
- bower-asset/twig.js: 0.8.4@stable
- yiisoft/yii2: ~2.0.0
- yiisoft/yii2-twig: dev-master
Requires (Dev)
- yiisoft/yii2-bootstrap: ~2.0.0
- yiisoft/yii2-faker: *
This package is not auto-updated.
Last update: 2024-09-14 19:36:40 UTC
README
#Yii2 Simple Chat 为您的yii2应用程序提供的简单聊天
##安装
安装此扩展的首选方式是通过 composer。
运行以下命令
php composer.phar require --prefer-dist gofmanaa/yii2-simplechat
或者在您的 composer.json
文件的 require 部分添加
"gofmanaa/yii2-simplechat": "~2.0",
to
##仅演示
安装扩展后,只需按以下方式修改您的应用程序配置
return [ 'bootstrap' => ['simplechat'], 'modules' => [ 'simplechat' => [ 'class' => 'bubasuma\simplechat\Module', ], // ... ], // ... ];
对于控制台应用程序使用相同的配置
注意:您需要此配置才能通过命令行访问简单聊天。在生产模式下您可以移除它。
您可以通过以下方式通过命令行访问 Simple Chat
# 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
您可以指定 start
和 reset
命令的不同选项
# 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"
然后,您可以通过以下 URL 访问 Simple Chat
https:///path/to/index.php?r=messages/2
或者如果您已启用漂亮的 URL,您可以使用以下 URL
https:///path/to/index.php/messages/2
您应该看到以下内容
如果不这样做,请检查是否已成功将演示迁移应用到您的数据库中。您可以通过运行以下命令来检查
php yii simplechat/start
注意:上述命令仅在您已按上述建议配置了您的控制台应用程序时才能访问。
##使用
创建一个类似于以下ActiveRecord的对象
namespace common\models; //... use bubasuma\simplechat\db\Model; use common\models\User; use yii\db\ActiveQuery; //... class Message extends Model { public function getContact() { return $this->hasOne(User::className(), ['id' => 'contact_id']); } /** * @inheritDoc */ public static function conversations($userId) { return parent::conversations($userId)->with([ //... 'contact' => function ($contact) { /**@var $contact ActiveQuery * */ $contact->with([ //... ])->select(['id', ]); }, //... ]); } }
创建一个类似于以下控制器
namespace frontend\controllers; //... use yii\web\Controller; use yii\helpers\StringHelper; use common\models\Message; use bubasuma\simplechat\controllers\ControllerTrait; //... class MessageController extends Controller { use ControllerTrait; /** * @return string */ public function getModelClass() { return Message::className(); } /** * @inheritDoc */ public function formatMessage($model) { //... return $model; } /** * @inheritDoc */ public function formatConversation($model) { //... $model['text'] = StringHelper::truncate($model['text'], 20); //... return $model; } }
注意:如果您在您的前端应用程序中使用此扩展,您可以在
index.twig
中找到小部件的使用方法。
##常见问题 此扩展是否与除 twig
之外的任何模板引擎兼容?
是的。鉴于 yii2
的默认渲染是 php
,您必须在视图名称中显式指定扩展部分。
我可以在 RESTful API 中使用此扩展吗?
是的,您可以使用。
我可以在服务器端和客户端使用不同的模板引擎进行渲染吗?
是的。但使用相同的模板在两端仍然是最佳实现。