dallin
dallin

Reputation: 9426

Composer not auto-loading required classes

I'm new to Composer and I'm really struggling to auto-load my classes with composer. What am I missing in the following process?

I installed the package in my PHP includes folder (which is outside the document root - I'm not sure if that matters) like this:

composer require monolog\monolog

It stated it completed successfully and I confirmed the project was added to my vendor folder.

My entire composer.json file looks like this:

{
    "require": {
        "monolog/monolog": "^1.22"
    }
}

My entire test file looks like this:

<?php

require_once "vendor/autoload.php";
use Monolog\Logger;

$log = new Logger("name");

?>

And I get this error when I load the page:

Fatal error: Uncaught Error: Class 'Monolog\Logger' not found in C:\Dropbox\Projects\Web\Websites\Instamation\wwwroot\qbtest.php:6 Stack trace: #0 {main} thrown in C:\Dropbox\Projects\Web\Websites\Instamation\wwwroot\qbtest.php on line 6

It includes the vendor/autoload.php file without any error.

I've tried to run these commands in composer without any change:

composer update
composer dump-autoload -0

I've also tried it with different packages and I get the same error, so I'm pretty sure it has nothing to do with the monolog package.

Is there a step here I'm missing? I don't need to manually define which classes to autoload in a json file if I require them in composer, do I?

Edit 1:

As requested, here's the paths to my different files.

Path to the test page:

C:\Dropbox\Projects\Web\Websites\Instamation\wwwroot\qbtest.php

Path to the composer.json file (outside the document root but in my includes path):

C:\Dropbox\Projects\Web\Websites\Instamation\wwwincludes\composer.json

My vendor folder is here:

C:\Dropbox\Projects\Web\Websites\Instamation\wwwincludes\vendor\

And inside my vendor folder I have these folders and file:

bin/
composer/
monolog/
psr/
autoload.php

Upvotes: 0

Views: 1283

Answers (1)

Alex Blex
Alex Blex

Reputation: 37048

You need to include autoload in your qbtest.php as following:

require_once "../wwwincludes/vendor/autoload.php";
use Monolog\Logger;

$log = new Logger("name");

Upvotes: 1

Related Questions