0

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
dallin
  • 8,775
  • 2
  • 36
  • 41
  • Please add to the question full path to composer.json and list of directories you have in `vendor` directory. – Alex Blex Jun 09 '17 at 21:28
  • @AlexBlex Thanks. I've added the information you've requested. Does anything look off? – dallin Jun 09 '17 at 21:46

1 Answers1

1

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

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

$log = new Logger("name");
Alex Blex
  • 34,704
  • 7
  • 48
  • 75
  • Wow! That worked! After digging into why, I found out that I had another location in my includes path I was unaware of, and that location had an old composer vendor file in it, so it was loading the wrong autoload.php. No matter what I did, it was always going to pick up that old autoload.php in my other includes location and would never load the correct one and work! THANK YOU SO MUCH! I've been working on this all day! – dallin Jun 09 '17 at 22:12