1. 程式人生 > 實用技巧 >thinkphp5.0報錯Class 'think\worker\Server' not found問題解決

thinkphp5.0報錯Class 'think\worker\Server' not found問題解決


環境
win7
thinkphp 5.0
think-worker 1.0.*

今天在thinkphp 5.0上使用workman

參照官網教程步驟如下

安裝think-worker 1.0.*版本

composer require topthink/think-worker 1.0.*

使用方法
首先建立控制器類並繼承 think\worker\Server,然後設定屬性和添加回調方法,支援workerman所有的回撥方法定義(回撥方法必須是public型別)

namespace app\index\controller;

use think\worker\Server;

class Worker extends
Server { protected $socket = 'http://0.0.0.0:2346'; public function onMessage($connection,$data) { $connection->send(json_encode($data)); } }

在應用根目錄增加入口檔案 server.php

#!/usr/bin/env php
<?php
define('APP_PATH', __DIR__ . '/application/');

define('BIND_MODULE','index/Worker');

// 載入框架引導檔案 require __DIR__ . '/thinkphp/start.php';

在命令列啟動服務端

php server.php start

linux下面可以支援下面指令

php server.php start|stop|status|restart|reload

執行php server.php start後報錯

[think\exception\ErrorException]
Class 'think\worker\Server' not found

找了一些資料,說的是composer問題、大小寫問題等等,都沒有解決

報錯原因分析:沒有載入到類think\worker\Server

解決辦法:

既然沒有載入到這個類,那我載入一下不就行了,修改server.php

server.php檔案程式碼:

#!/usr/bin/env php
<?php
define('APP_PATH', __DIR__ . '/application/');

define('BIND_MODULE','index/Worker');

//載入composer autoload檔案
require __DIR__ . '/vendor/autoload.php';

// 載入框架引導檔案
require __DIR__ . '/thinkphp/start.php';

儲存後,執行php server.php start,出現如下內容:

----------------------- WORKERMAN -----------------------------
Workerman version:3.5.30          PHP version:5.6.9
------------------------ WORKERS -------------------------------
worker               listen                              processes status
none                 http://0.0.0.0:2346                 4         [ok]

成功開啟websocket服務,問題解決。

參考文件:
1.think-worker 1.0官網使用教程
https://github.com/top-think/think-worker/tree/v1.0.1