名称空间错误。卡在 'MyApp\Chat'

NameScape Error. Stuck in 'MyApp\Chat'

在 Ratchet 简单示例中出现错误:

Fatal error: Uncaught Error: Class 'MyApp\Chat' not found

我的文件结构如下:

root\composer.json 
root\bin\chat-server.php 
root\src\MyApp\Chat.php

Composer.json 是

{
    "autoload": {
        "psr-0": {
            "MyApp": "src"
        }
    },
    "require": {
        "cboden/ratchet": "0.3.*"
    }
}

聊天-server.php是

<?php
use Ratchet\Server\IoServer;
use MyApp\Chat;

    require dirname(__DIR__) . '/vendor/autoload.php';

    $server = IoServer::factory(
        new Chat(),
        8080
    );

    $server->run();

并且Chat.php是

<?php
namespace MyApp;
use Ratchet\MessageComponentInterface;
use Ratchet\ConnectionInterface;

class Chat implements MessageComponentInterface {
    public function onOpen(ConnectionInterface $conn) {
    }

    public function onMessage(ConnectionInterface $from, $msg) {
    }

    public function onClose(ConnectionInterface $conn) {
    }

    public function onError(ConnectionInterface $conn, \Exception $e) {
    }
}

我试过以下方法,但现在仍然有效: PHP Fatal error: Class 'MyApp\Chat' not found in /MyApp/chat-server.php

检查你的自动加载命名https://getcomposer.org/doc/04-schema.md#psr-0

应该是这样的:

{
    "autoload": {
        "psr-0": {
            "MyApp\": "src/"
        }
    },
    "require": {
        "cboden/ratchet": "0.3.*"
    }
}

然后做

composer dump-autoload