3
votes

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.

2
Have you tried finishing all the folder names with a /? That's the only difference I see to my own codeNico Haase
NicoHaase Didn't change anything.Jean-Marc Zimmer
How does src/classes/Application.php looks like?rob006
Can you run composer dump-autoload --classmap-authoritative? This should give you the result you expect. Right now it will not read the files individually, only the register the directory mappings.dbrumann
I created the same folder structure with just empty class skeletons and it works for me. See: imgur.com/a/xM0X5r9 Could you maybe show the full composer.json? Maybe that can help identify the issuedbrumann

2 Answers

1
votes

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.

1
votes

Ensure your composer.json references your deployment paths. For example:

Dockerfile

FROM php:7.2-apache

COPY src /var/www/html
COPY vendor /var/www/vendor

composer.json

{
  "autoload": {
    "psr-4": {
      "Acme\\": "html/classes/"
    }
  }
}

i.e. html/classes/ not src/classes/