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
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
Related
I have created a custom comoposer package and I want to use it on my project with this composer.json:
{
"name": "papillon/test",
"type": "library",
"version": "dev-master",
"require": {
"php": "^7.1.11"
},
"autoload": {
"psr-4": {
"Papillon\\Fountaine\\Eau\\": "src/Papillon/Fountaine/Eau/"
}
}
}
I compress it in zip. In the main project, I add a folder called repo, where I add de composer package zip. Then, I modify the composer.json of the main project like this:
{
"repositories": [
{
"type": "artifact",
"url": "var/main/repo"
}
],
"require": {
"papillon/test": "dev-master"
}
}
I execute composer update and the pakage is added to vendor folder; all seems to be going well... but if I want to test the package from the main project with this script:
<?php
require (__DIR__ . '/vendor/autoload.php');
use Papillon\Fountaine\Eau\FlowerClass;
echo FlowerClass::bloom();
It returns: PHP Fatal error: Uncaught Error: Class 'Papillon\Fountaine\Eau\FlowerClass' not found in .../test_package.php:6
Stack trace:
#0 {main}
thrown in .../test_package.php on line 6
I think that the package may not be recognized by the main project; maybe the package was improperly installed in the main project?
Debugging autoload can be very useful to catch errors. Take care with the route paths, the autoload tryed to find the classes files in a path with a lowercase folder when in the package composer.json the route was definded with that folder uppercase.
i've already been looking for solutions to my problem but could'nt find any so far.
{
"name": "petersil98/thresh",
"version": "1.0.0",
"type": "library",
"autoload": {
"psr-4": {
"Thresh\\": "src/"
}
}
}
This is my public/test.php:
<?php
require_once '../vendor/autoload.php';
use Thresh\Helper\Config;
Config::setPlatform("euw1");
And this is my src/Helper/Config.php:
<?php
namespace Thresh\Helper;
class Config{...}
This is the error i get: Fatal error: Uncaught Error: Class 'Thresh\Helper\Config' not found
First i had my psr-4 autoload registered with the prefix 'src'
"autoload": {
"psr-4": {
"src\\": "src/"
}
}
After changing the psr-4 autoload prefix to Thresh (and updating the namespaces) and running composer dump-autoload it doesnt work anymore
PS: composer dump-autoload returns Generated autoload files containing 0 classes
I have been trying to use namespaces for the first time in ages and I am running into the below problem. I am currently using Composer for a PSR-4 autoloader and I keep getting the error:
Fatal error: Class 'API\Library\Config' not found in C:\wamp64\www\project\src\index.php on line 14
composer.json
"autoload": {
"psr-4": {
"API\\": "src",
"API\\Library\\": "src/Library",
"API\\Controllers\\": "src/Application/Controllers"
}
}
src/index.php
namespace API;
include_once('vendor/autoload.php');
use API\Library\Config;
$config = new Config(); //line 18
The Folder layout is as such:
Its because src is the parent folder. Ideally vendor would be in the same directory as src.
"autoload": {
"psr-4": {
"API\\": "",
"API\\Library\\": "Library",
"API\\Controllers\\": "Application/Controllers"
}
}
Would work, or you should restructure your directories.
Also you can leave out "API\\Library\\": "Library", as it will be picked up by "API\\": "",
When running codecept run -vvv I get an error stating that the actor is undefined in the SuiteManager class (method is initialize).
Any guess on how to fix this?
I've set up two basic unit tests where I just want to assert some values, and they worked until I switched to the composer autoloader.
In the composer.json I have the following:
"autoload": {
"classmap": [
"app"
]
},
"require-dev": {
"codeception/codeception": "^2.3",
"phpstan/phpstan": "^0.8.0",
"symfony/console": "^3.3"
}
In the main _bootstrap file provided by Codeception I only have a require_once for the vendor autoloader file.
Using php 7.1 and latest versions of the packages.
I get this error when I try to use autoload and namespaces. All my namespace classes are under app/libs/
16-Dec-2016 04:30:50 Europe/Berlin] PHP Fatal error:
Class 'App\libs\App' not found in /Users/mysite/app/page1.php on line 26
Here is My code:
require '../public/vendor/autoload.php';
use App\libs\App;
use App\libs\Auth;
class Controller
{
public $app;
public function __construct()
{
#set_exception_handler([$this, 'exceptionHandler']);
$this->app = new App();
}
}
autoload normally includes files only under vendor folders. It does not load any other files, if you do not instructe to. You are probably using composer. if it is, you can add folders in composer.json file to include class files from other folders like App\libs. An example a composer.json file is:
{
"require": {
"twig/twig": "~1.0"
},
"autoload": {
"psr-4": {
"App\\": "App/"
}
}
}
In the examle above, it will autoload any files under App folder.
Finally you need to run: composer dump-autoload to make this working.