Sundar
Sundar

Reputation: 4650

PHP Fatal error: Class 'MyApp\Chat' not found in /MyApp/chat-server.php

I am trying to run the Ratchet application demo but I can't execute the file

This is my file structure

/var/www/src/MyApp/
/var/www/src/MyApp/chat.php
/var/www/src/MyApp/chat-server.php
<?php
use Ratchet\Server\IoServer;
use MyApp\Chat;

#require "chat.php";

    require 'vendor/autoload.php';

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

    $server->run();
/var/www/src/MyApp/composer.json
{
    "autoload": {
        "psr-0": {
            "MyApp": "src"
        }
    },
    "require": {
        "cboden/Ratchet": "0.3.*"
    }
}

Vendor Folder is exist in this location

/var/www/src/MyApp/vendor/

Whenever I am executing the chat-server file in terminal I got the following error

PHP Fatal error:  Class 'MyApp\Chat' not found in /MyApp/chat-server.php

Please help me how to resolve this

Note: The complete code details are exist in this page

http://socketo.me/docs/hello-world

This question was asked but still no answer for that question too Class 'MyChat\Chat' not found in C:\wamp\www\bin\chat-server.php

Upvotes: 18

Views: 16892

Answers (7)

Kwadz
Kwadz

Reputation: 2232

Here is my working configuration:

root
├── bin
│   └── chat-server.php
├── src
│   └── Chat.php
├── composer.json
├── composer.lock
└── vendor
{
    "autoload": {
        "psr-4": {
            "MyApp\\": "src"
        }
    },
    "require": {
        "cboden/ratchet": "^0.4"
    }
}

The tutorial does not mention that after adding/updating the autoload section in composer.json you need to run a composer install to regenerate autoload files.

Upvotes: 3

Amir Hajiha
Amir Hajiha

Reputation: 935

In my case, after spending half a day, I realised the error was because I missed this line:

require 'vendor/autoload.php';

Upvotes: 0

Peter Binder
Peter Binder

Reputation: 11

I wrestled with this issue for hours and found that I was missing a few packages. I was able to fix it with this command:

php composer.phar install --no-dev -o

Here's a picture of what I saw after running: installed packages

Hope this helps!

Upvotes: 1

Jaskaran Singh
Jaskaran Singh

Reputation: 2568

You have to mention Base Path of your classes that need to be autoloaded:

{
    "autoload": {
        "psr-4": {
            "MyApp\\": "src/MyApp/"
        }
    },
    "require": {
        "cboden/ratchet": "^0.4.0",
        "react/zmq": "0.2.*|0.3.*"
    }
}

And run

Composer dump-autoload

Upvotes: 10

Pransh Tiwari
Pransh Tiwari

Reputation: 4182

Try autoloading the files first with:

$ composer update

If it still doesn't work then include the line require 'chat.php';, just at the beginning of the chat-server.php file. It worked for me.

Upvotes: 8

dearsina
dearsina

Reputation: 5192

If you don't have the composer.json file and the vendor folder at root, it won't work. Also, don't touch the vendor folder (and subfolders). The folder structure must be like this:

/composer.json
/composer.phar
/vendor/[misc]
/src/MyApp/Chat.php

Finally, after updating the composer.json, make sure to run an update:

php composer.phar update

This way it will work.

Upvotes: 19

Wouter J
Wouter J

Reputation: 41934

The main path for the autoloading is the location of the composer.json file, so if that lives in /var/www/src/MyApp/, the autoloading will use that as a base.

In your case, you say the MyApp namespace can be found in the src directory (which means /var/www/src/MyApp/src). That's not true, since the file /var/www/src/MyApp/src/MyApp/Chat.php does not exists.

You can solve this issue in 3 different ways:

  • Moving composer.json - You can move the composer.json file to /var/www, to be able to use that as base;
  • Updating autoloading (using PSR-4) - You can also use PSR-4 instead of PSR-0 and configure it as "autoload": { "psr-4": { "MyApp\\": "" } };
  • Reorganizing your files - You can also change the file structure to:

    /var/www/src/MyApp/
        src/MyApp/
                Chat.php
        composer.json
    

Upvotes: 11

Related Questions