I'm new to composer to manage the loading of my custom classes. I'm trying to add some custom classes to the autoload but without success, I will get always a Class not found...
error.
How I will setup correctly composer, where I should put the composer.json
file with the psr-4 info about my custom class/classes?
Can anyone help me understand how it will work in this case?
Here is my class code snippet, I'm using some composer packages so I need to autoload them. This file is placed in it's own directory. th structure looks like this: project_root\assets\library\MyClassFolder
<?php
namespace MyNamespace;
require_once __DIR__.'/vendor/autoload.php';
use \Foo\Bar;
class MyClass {
...
}
?>
This is the code where the class need to be loaded. This file is located inside my project root folder and it's causing the error:
<?php
require_once __DIR__.'/vendor/autoload.php';
use \MyNamespace\MyClass;
if( isset($_POST['do_action']) ){
MyClass::init();
}
?>
This is the composer.json file that is in the project root:
{
"require": {
"gabordemooij/redbean": "^5.3"
},
"autoload": {
"psr-4": {
"MyNamespace\\": "assets/library/"
}
}
}
MyNamespace
, then your folder should not be namedMyClassFolder
– Nico Haase/controller.php - Uncaught Error: Class 'MyNamespace\MyClass' not found
– Oshione