I was making a composer library for functionality of my framework, but I got stuck on this problem, the problem is with autoloading with psr-4.
My Configuration
I have the following structure
├───src
├───test
└───vendor
├───composer
├───symfony
│ ├───polyfill-ctype
│ └───polyfill-mbstring
│ └───Resources
│ └───unidata
├───twig
│ └───twig
│ ├───doc
│ │ ├───filters
│ │ ├───functions
│ │ ├───tags
│ │ └───tests
│ └───src
│ ├───Cache
│ ├───Error
│ ├───Extension
│ ├───Loader
│ ├───Node
│ │ └───Expression
│ │ ├───Binary
│ │ ├───Filter
│ │ ├───Test
│ │ └───Unary
│ ├───NodeVisitor
│ ├───Profiler
│ │ ├───Dumper
│ │ ├───Node
│ │ └───NodeVisitor
│ ├───RuntimeLoader
│ ├───Sandbox
│ ├───Test
│ ├───TokenParser
│ └───Util
└───xenframe
└───hello
└───src
The last folder is my library with the hello folder in it.
inside of the root of the hello folder I have a composer.json
and a src
folder.
composer.json
{
"name": "xenframe/hello",
"type": "library",
"license": "MIT",
"authors": [
{
"name": "XENONMC",
"email": "support@xenonmc.xyz"
}
],
"minimum-stability": "stable",
"require": {
},
"autoload": {
"psr-4": {
"hello\\": "/src"
}
}
}
Inside of the src/
folder is the main file - > index.php
.
index.php
<?php
namespace xenframe\hello;
class App {
function __construct() {
echo "object constructed";
}
}
echo "hello world was loaded";
Now, the usage is in my root index.php
.
index.php
<?php
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
$root = str_replace('\\', '/', __DIR__);
// setup composer autoloader
require_once $root . '/vendor/autoload.php';
// visit us as https://xenonmc.xyz
use xenframe\hello\App;
$hi = new App();
What I tried
o) used psr-4 like the following "xenframe\\hello": "src/"
.
o) used class paths, I don't like this one because psr-4 is the recommended standard of composer.
o) using psr-0, I didn't pick this one either as there was a lot of verbose.
Thanks!