'Composer configuration for directory root
I am using composer for the first time, however the configuration confuses me a lot.
Project structure
- myproject
- composer/
- composer.phar
- dist/ <-- Directory root
- index.php
- server/ <-- my server code (php)
- Controller/
- TestController.php
- Test/
- Advanced/
- AdvancedTest.php
- Standard/
- StandardTest.php
- src/ <-- My client source files (typescript, sass, ...)
- vendor/ <-- Directory created by composer
- composer.json
Examples
TestController.php:
<?php
declare(strict_types=1);
namespace Controller;
class TestController
{
public function __construct()
{
echo "Test controller initialized.<br>";
}
}
AdvancedTest.php:
<?php
declare(strict_types=1);
namespace Test\Advanced;
class AdvancedTest
{
public function __construct(string $title)
{
echo "Advanced test: " . $title . "<br>";
}
}
StandardTest.php:
<?php
declare(strict_types=1);
namespace Test\Standard;
class StandardTest
{
public function __construct(string $title)
{
echo "Basic test: " . $title . "<br>";
}
}
index.php:
<?php
include_once '../vendor/autoload.php';
use Controller\TestController;
use Test\Advanced\AdvancedTest;
use Test\Standard\StandardTest;
$standardTest = new StandardTest("HelloWorld");
$advancedTest = new AdvancedTest("HelloWorld");
$testController = new TestController();
This is my composer.json:
{
"autoload": {
"psr-4": {
"Controller\\": "server",
"Test\\": "server"
}
}
}
When I run composer, it throws
Class Test\Advanced\AdvancedTest located in C:/project1/web/server\Test\Adanced\AdvancedTest.php does not comply with psr-4 autoloading standard. Skipping.
Class Test\Standard\StandardTest located in C:/project1/web/server\Test\Standard\StandardTest.php does not comply with psr-4 autoloading standard. Skipping.
Class Controller\TestController located in C:/project1/web/server\Controller\TestController.php does not comply with psr-4 autoloading standard. Skipping.
So PHP cannot find the classes that are used in index.php.
What I want to do:
dist/index.phpin the directory root is my starting point. I need the autoload script there.- I want to autoload 1 and 2 level classes (e.g. Test 2 level, Controller 1 level) in my project.
I did not found a good description yet, how to archieve this with composer.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
