I am trying to improve my file structure, as my validators folder is quickly filling up. To improve this, I want to add new sub-folders inside this folder in order to keep everything organised and manageable. However, in doing so, my application no longer correctly autoloads all the required files. Specifically, it misses out any files inside a sub-folder of /validators (see structure below)
v1
|-- validators
|-- sessionValidators
The autoload part of my composer.json file looks like this:
"autoload": {
"classmap": [
"./",
"v1",
...
"v1/validators",
"v1/validators/sessionValidators"
]
}
I am unsure if having v1/validators and v1/validators/sessionValidators might be the issue.
I have tried removing v1/validators/sessionValidators from composer.json, but of course that did not work.
To be clear, everything was being loaded just fine before I tried moving things into the new sessionValidators folder.
The Slim code with is unable to find the SessionValidator class looks like this:
$app->put('/session', 'SessionResource::updateSession')->add('SessionValidator');
I have tried changing it to this, to no avail:
$app->put('/session', 'SessionResource::updateSession')->add('sessionValidators/SessionValidator');
I can get the code to execute by adding this to the top of the Slim routing file:
require_once 'validators/sessionValidators/SessionValidator.php';
However, this is not an elegant solution as I would have to do this for every single file in a folder inside /validators, and there's a lot of those.
I've seen repeatedly that regenerating the autoload file should solve this issue, however I don't think that's the case here. When I re-run my docker-compose the autoload file should be re-generated automatically; see the Docker files at the end of this post.
Everything aside from v1/validators/sessionValidators seems to be loaded correctly. When I try to make a request or run a test which requires a class inside the sessionValidators folder, I get this error:
/var/www/html/vendor/php-di/invoker/src/Exception/NotCallableException.php (33) 'SessionValidator' is neither a callable nor a valid container entry #0 /var/www/html/vendor/php-di/invoker/src/CallableResolver.php(80)
If it happens to be relevant, I am running all this inside a Docker container, though I don't believe that will be the issue.
docker-compose.yml
version: '3.3'
services:
salus-db:
image: postgres:11.2
volumes:
- ./tests:/docker-entrypoint-initdb.d/
ports:
- 7000:5432
salus-api:
build: .
volumes:
- .:/var/www/html
- ${CONFIG_PATH}:/var/www/html/conf
- ${HOST_LOG_PATH}:/var/www/html/salus_api_log
env_file:
- api.env
ports:
- 9191:80
depends_on:
- salus-db
links:
- salus-db:salus-db
Dockerfile
# Build Dockerfile from official PHP 7.3 image
FROM php:7.3-apache
# Create new user 'deploy'
RUN useradd -ms /bin/bash deploy
# Copy contents of current directory into /var/www/html/ inside the container
COPY . /var/www/html/
# Install Composer
RUN curl -sS https://getcomposer.org/installer | \
php -- --install-dir=/usr/bin/ --filename=composer
USER deploy
# Install Composer dependencies without autoloader
RUN composer install --no-autoloader
# Copy new contents
COPY . ./
# Generate autoload.php
RUN composer dump-autoload
USER root
ADD apache-config.conf /etc/apache2/sites-enabled/000-default.conf
# Expose port 80 inside the container
EXPOSE 80