Morpheus_ro Morpheus_ro -4 years ago 142
PHP Question

Silex command fail

I have this error when I try to run a console command with Silex.

PHP Error: Class 'Testing\Command\TestingCommand' not found in /var/www/testCmd/app/console on line 9
PHP Stack trace:
PHP 1. {main}() /var/www/testCmd/app/console:0
PHP Fatal error: Uncaught Symfony\Component\Debug\Exception\ClassNotFoundException: Attempted to load class "TestingCommand" from namespace "Testing\Command".
Did you forget a "use" statement for another namespace? in /var/www/testCmd/app/console:9
Stack trace:
#0 {main}
thrown in /var/www/testCmd/app/console on line 9


I have app/console.php and app/bootstrap.php files. The console is loading the bootstrap and in the console file I have some thing like:

#!/usr/bin/env php
<?php

set_time_limit(0);

$app = require_once __DIR__ . '/bootstrap.php';

$application = $app['console'];
$app['console']->add(new \Testing\Command\TestingCommand());
$application->run();


composer file

{
"name": "testing/Command",
"require": {
"knplabs/console-service-provider": "^2.0",
"silex/silex": "^2.0",
"symfony/monolog-bridge": "^3.1",
"doctrine/common": "^2.6",
"doctrine/dbal": "^2.5"
},
"autoload": {
"psr-4": {
"\\": "src/"
}
}
}


The command is located in src/Command/TestingCommand.php

I am super new to Silex and I don't know what can cause the issue. Thank you

Answer Source

Autoloader can't load command class. According to autoload section of composer.json and class name file with this class should be located in src/Testing/Command/TestingCommand.php. So you can move this file in this location or set another search directory in composer.json:

"autoload": {
    "psr-4": {
        "Testing\\Command\\": "src/Command/"
        "\\": "src/"
    }
}

after changing composer.json run composer dump-autoload

https://getcomposer.org/doc/01-basic-usage.md#autoloading

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download