I am trying to understand the autoloading part with composer. Attached image is my project structure.
index.php
require __DIR__ . '/vendor/autoload.php';
// require "app/Controller/MyController.php"; // If i uncomment this my code is working fine. (only for testing purpose)
$mynamespace = new App\Controller\MyController();
$mynamespace->index();
composer.json
"psr-4": {
"App\\": "app/"
}
MyController.php
namespace App\Controller;
class MyController{
public function index(){
echo "New World";
}
}
I ran composer dump-autoload and the file is not loaded.
How to map the autoloaded file via composer? I might go with multiple folders and files. So i prefer it to be one single directory as app/
Off the top of my head, so not sure if helpful:
require __DIR__ . '/vendor/autoload.php';
use App\Controller\MyController;
$mynamespace = new MyController();
$mynamespace->index();
or possibly escape the top level namespace:
$mynamespace = new \App\Controller\MyController();
{
"name": "alaksandarjesus/testnamespace",
"authors": [{
"name": "alaksandarjesus",
"email": "abc@yahoo.co.in"
}],
"require": {},
"autoload": { // Missed this autoload and so it didnt load.
"psr-4": {
"App\\": "app"
}
}
}