GatewayWorker與tp5.0框架結(jié)合
下載DEMO后, 把DEMO放到了extend下面,現(xiàn)在可以調(diào)用GatewayWorker的函數(shù), 但Evnets里面寫框架的函數(shù)就會(huì)提示找不到, 有什么辦法或文檔嗎? 查了一天沒找到方案
不是把兩個(gè)框架代碼放一起就能直接整合了,那樣的話我可以把tp和laravel框架代碼放一起,就直接能用二者的所有特性了?
如果你不熟悉兩個(gè)框架內(nèi)部執(zhí)行機(jī)制流程,整合是很困難的。其實(shí)大部分情況沒必要把2者代碼揉在一起,比如你可能只需要tp的數(shù)據(jù)庫用法,那直接在gatewayWorker里composer安裝tp的orm就行了。
安裝tp數(shù)據(jù)庫orm
composer require topthink/think-orm
加載composer的autoload.php
start.php里頭部加上
// vendor是執(zhí)行composer require topthink/think-orm 后生成的目錄
require_once __DIR__ . '/vendor/autoload.php';
使用
use think\facade\Db;
class Events {
public static function onWorkerStart()
{
Db::setConfig([
// 默認(rèn)數(shù)據(jù)連接標(biāo)識(shí)
'default' => 'mysql',
// 數(shù)據(jù)庫連接信息
'connections' => [
'mysql' => [
// 數(shù)據(jù)庫類型
'type' => 'mysql',
// 主機(jī)地址
'hostname' => '127.0.0.1',
// 用戶名
'username' => 'root',
// 數(shù)據(jù)庫名
'database' => 'demo',
// 數(shù)據(jù)庫編碼默認(rèn)采用utf8
'charset' => 'utf8',
// 數(shù)據(jù)庫表前綴
'prefix' => 'think_',
// 數(shù)據(jù)庫調(diào)試模式
'debug' => true,
],
],
]);
}
public static function onMessage($client_id, $data)
{
Db::table('xxx')->xxxx....;
}
}
thinkphp-orm文檔 https://www.kancloud.cn/manual/think-orm/1257998
如果你需要tp的其它功能,也可以到tp的代碼庫里找,然后composer安裝使用。
tp的其它功能組件地址 https://github.com/top-think