Morpheus_ro
Morpheus_ro

Reputation: 545

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

Upvotes: 1

Views: 343

Answers (1)

Max P.
Max P.

Reputation: 5679

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

Upvotes: 1

Related Questions