Search code examples
composer-phpautoloader

What is wrong in my composer psr-4 autoload?


I'm creating a web app with Slim and Twig. The libraries I use work perfectly, I can call them easily with no problem. However my own classes are not found by composer.json autoload psr-4 (psr-0 doesn't find them either)
Here is my file system:

project
   |composer.json
   |src
       |public
       |   |index.php
       |classes
       |   |Application.php
       |   |middlewares
       |       |SecurityMiddleware.php
       |templates
           |TemplateController.php
           |main
               |MainController.php

Here is my composer.json:

{
    "authors": [
        {
            "name": "Jean-Marc ZIMMER",
            "email": "################@gmail.com",
            "role": "Developer"
        }
    ],
    "require": {
        "slim/slim": "^3.11",
        "slim/extras": "*",
        "twig/twig": "^2.5",
        "slim/twig-view": "^2.4",
        "slim/views": "^0.1.3"
    },
    "autoload": {
        "psr-4": {
            "src\\": "src",
            "middlewares\\": "src/classes/middlewares",
            "classes\\": "src/classes",
            "templates\\": "src/templates"
        }
    }
}

Then src/classes/Application.php:

<?php

namespace classes;

class Application extends \Slim\App {

    public function __construct($container = array()) {
        parent::__construct($container);
    }
}

And finally my index.php file:

<?php

require '../../vendor/autoload.php';

$app = new \classes\Application([
    "settings" => [
        "displayErrorDetails" => true
    ]
]);
$app->run();

When I run composer dump-autoload, the command outputs:

Generated autoload files containing 0 classes

then exits with status code 0. It should find 4 classes, right ?
And running the app shows the error:

Fatal error: Uncaught Error: Class 'classes\Application' not found in /opt/lampp/htdocs/project/src/public/index.php:5

I'm sure I'm missing something, indicating a namespace or something. Can anyone help me ?

Edits:
I tried using the --optimize or the --classmap-authoritative option for dump-autoload. Changed nothing.
Adding a '/' to the folder names in composer.json doesn't change anything.


Solution

  • I got a solution from another source. I don't personally like it, but it works.
    The file system wasn't changed.

    composer.json autoload:

    "autoload": {
        "psr-4": {
            "App\\": "src/"
        }
    }
    

    src/public/index.php:

    <?php
    
    require '../../vendor/autoload.php';
    
    $app = new \App\classes\Application([
        "settings" => [
            "displayErrorDetails" => true
        ]
    ]);
    $app->run();
    

    src/classes/Application.php:

    <?php
    namespace App\classes;
    
    class Application extends \Slim\App {
    
        public function __construct($container = array()) {
            parent::__construct($container);
        }
    }
    

    I'm going to work from this functional base and see if I can get the result I want. If I do, I'll edit this answer.