Shawn Lutch
Shawn Lutch

Reputation: 13

PHP doesn't find class from library that should be auto-loaded by Composer

PHP doesn't seem to find a class defined in a library which I'm trying to auto-load from Composer.

I'm messing around with PHP served by Apache 2.4, which I've had success with in the past, but introducing Composer into the mix has proven troublesome for me. I'm attempting to use an OAuth2 library for Slack, installed successfully using composer require bramdevries/oauth2-slack in the virtualhost's document root, such that the file structure looks like:

+-- composer.json
+-- composer.lock
+-- index.php
+-- vendor/
|  +-- autoload.php
|  +-- composer/
|  +-- bramdevries/
|  |   +-- <library files>
|  ... dependencies

This one library is the only one that I'm using thus far, and my index.php is the only source file in the entire document root that isn't a library inside composer/. This project is, for all intents and purposes, about 3 lines of PHP code, shown below.

From my understanding of the Composer documentation: since I'm using a library that provides its own composer.json, Composer should take care of generating all of the auto-loading stuff if I do composer dump-autoload (which, I gather, is unnecessary since Composer generates that stuff on update, install, etc).

However, whenever I go to load my index.php in a browser, Apache2 responds with HTTP 500.

Given the file structure above, here is the entirety of my index.php:

<?php
require "vendor/autoload.php";
$slack = new League\OAuth2\Client\Provider\Slack([ /* stuff */ ]);
?>

And here is the error found in /var/log/apache2/error.log:

PHP Fatal error: Uncaught Error: Class 'League\\OAuth2\\Client\\Provider\\Slack' not found in <document_root>/index.php:3
Stack trace:
#0 {main}
  thrown in <document_root>/index.php on line 3

So, I'm clearly doing something wrong, but what?

I've used Apache2 and PHP for a little while now (including a project for a pre-capstone CS project class this semester), but my working knowledge of PHP is limited to the functional aspects; I haven't messed much with its OOP aspects. Also, I'm familiar with dependency/package managers similar to Composer (specifically, NPM for Node.js), but not with Composer itself. TIA!

Edit: forgot to add – I've searched around enough that every link on the first couple pages of any given Google search with related terms shows up purple, and nothing I've tried thus far has worked for me, so I'm sure I'm just missing a step or something.

Upvotes: 1

Views: 81

Answers (1)

drew010
drew010

Reputation: 69937

Their example appears to be incorrect, or maybe changed and they forgot to update the README.

If you look at their source for the Slack provider, it is in the namespace Bramdevries\Oauth\Client\Provider.

You just need to change the constructor to:

$slack = new Bramdevries\Oauth\Client\Provider\Slack([ /* stuff */ ]);

Hopefully after that you don't run into anything else!

It makes use of the League code which their composer.json does bring in, but to use this particular provider you have to reference their code in the proper namespace.

The other hint at this is looking at their autoload definition in composer.json:

"autoload": {
    "psr-4": {
        "Bramdevries\\Oauth\\Client\\": "src"
    }
},

Upvotes: 2

Related Questions