我正在使用 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;
这是带有类映射的 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
}
类套接字驻留在应用程序/库/套接字中.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
这是套接字服务器的第 5 行:
use 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');
所有类和模型都存在,并且在此类中不使用时正常工作。
我已经尝试了很多事情,到时候这里可能会有一些奇怪的东西......
你的问题有点令人困惑,但听起来你对命名空间的工作原理有误解。
这
PHP 警告:非复合名称 'Socket' 的 use 语句在第 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 致命错误:在第 15 行的/home/michael/PhpstormProjects/preisopt/app/libraries/Socket.php 中找不到类"Eloquent">
PHP 致命错误:在第 35 行的/home/michael/PhpstormProjects/preisopt/app/libraries/Socket.php 中找不到类"User">
Make 听起来像您正在尝试在命名空间文件中使用全局类'Eloquent
和'User
。 您需要添加一个
use 'Eloquent;
use 'User;
,或者在使用这些文件时使用前导反斜杠引用这些全局类。