我正在使用Ratchet和Laravel创建一个websocket应用程序,并且需要在我的websocket类中使用Eloquent查询,但是由于我在websocket类中使用了另一个命名空间,因此eloquent不可用。
这是课程的开头:
use Illuminate\Auth\UserTrait;
use Illuminate\Auth\UserInterface;
use Illuminate\Auth\Reminders\RemindableTrait;
use Illuminate\Auth\Reminders\RemindableInterface;
use Ratchet\MessageComponentInterface;
use Ratchet\ConnectionInterface;
class Socket extends Eloquent implements MessageComponentInterface, UserInterface, RemindableInterface {
use UserTrait, RemindableTrait;
这是带有classmap的composer.json:
"autoload": {
"classmap": [
"app/commands",
"app/controllers",
"app/models",
"app/database/migrations",
"app/database/seeds",
"app/tests/TestCase.php",
"app/libraries",
"vendor/cboden/ratchet/src" // added this
]
我已将composer.json从原始状态更改为上面的状态,就像:
...
"psr-0": { //removed this
"MyApp": "vendor/cboden/ratchet/src", //removed this
}
类Socket位于app / libraries / Socket.php
中 嗯,我已经尝试了一些我想到的东西,有人知道我做错了吗?抛出的错误是:
PHP Warning: The use statement with non-compound name 'Socket' has no effect in /home/michael/PhpstormProjects/preisopt/app/libraries/Socket-Server.php on line 5
PHP Fatal error: Class 'Eloquent' not found in /home/michael/PhpstormProjects/preisopt/app/libraries/Socket.php on line 15
这是Socket-Server的第5行:
use Socket;
和Socket的第15行:
class Chat extends Eloquent implements MessageComponentInterface, UserInterface, RemindableInterface {
我也尝试写过像
这样的行class Chat extends \Eloquent implements MessageComponentInterface, UserInterface, RemindableInterface {
这会抛出此错误:
PHP Fatal error: Class 'User' not found in /home/michael/PhpstormProjects/preisopt/app/libraries/Socket.php on line 35
这是第35行:
$dbtoken = User::where('username', '=', $split[1] )->get('sockettoken');
所有类和模型都存在且在本课程中未使用时正在运行。
我已经尝试了很多东西,到时候这可能是一些奇怪的东西......
答案 0 :(得分:2)
您的问题有点令人困惑,但听起来您似乎对命名空间的工作原理产生了误解。
这个
PHP警告:具有非复合名称的使用声明'套接字'在第5行的/home/michael/PhpstormProjects/preisopt/app/libraries/Socket-Server.php中没有效果
PHP是否告诉您该声明
use Socket;
没有做任何事情。 use
语句允许您告诉PHP"嘿,将此类从其他名称空间导入当前名称空间"。当您说use Socket
时,该类Socket
已经在当前名称空间中。如果您试图在另一个班级中使用全局班级\Socket
,您想说
use \Socket;
如果您尝试在基本名称为Socket
的其他命名空间中使用某个类,则您想说
use Namespace\Path\To\Socket
//or
use \Top\Level\Namespace\Path\To\Socket
取决于文件的当前名称空间。
错误
PHP致命错误:Class' Eloquent'在第15行的/home/michael/PhpstormProjects/preisopt/app/libraries/Socket.php中找不到
PHP致命错误:类'用户'在第35行的/home/michael/PhpstormProjects/preisopt/app/libraries/Socket.php中找不到
Make听起来像是在尝试在命名空间文件中使用全局类\Eloquent
和\User
。您需要添加
use \Eloquent;
use \User;
位于文件顶部,或者在您使用文件时使用文件中的前导反斜杠引用这些全局类。