Laravel 5.2 and Ratchet Class not found

问题描述:

我正在使用Ratchet for websockets。它一般工作,但我想在我的ExampleController Laravels Auth中使用。它应该很容易,但是,这并不工作:Laravel 5.2 and Ratchet Class not found

<?php namespace Annotation\Http\Controllers; 
use Auth; 
use Ratchet\MessageComponentInterface; 
use Ratchet\ConnectionInterface; 

class ExampleController extends Controller implements MessageComponentInterface { 

    public function onOpen(ConnectionInterface $conn) { 
     echo Auth::id(); 

    //etc. 
     echo "New Connection! ({$conn->resourceId})"; 
    } 

} 

我总是得到一个Auth类未发现异常,当我初始化我在WebSocket的-server.php(位于laravel的根目录)控制器以下文件:

<?php 
require __DIR__.'/vendor/autoload.php'; 

use Ratchet\Server\IoServer; 
use Annotation\Http\Controllers\CollaborativeController; 

$server = IoServer::factory(
    new ExampleController(), 
    8080 
); 
$server->run(); 

如果用我ExampleController与路由通常控制器的验证类会被发现。 (我也无法使用auth帮手或与laravel相关的任何东西)

为什么会发生这种情况?因为Laravel还没有初始化,还是需要添加路径?

auth函数返回一个验证器实例。你可以用它代替验证幌子,为方便:

echo auth()->user()->id; 
+0

这是行不通的,因为辅助类和laravels未加载IOC。 – Marco

以下行添加到您的server.php如果要加载中间件。 Auth是一个中间件,它不会被初始化和加载。

require __DIR__.'/../bootstrap/autoload.php'; $app = require_once __DIR__.'/../bootstrap/app.php'; (set a proper path to your bootstrap)