问题
I am trying to get the composer autoloader to work for an hour now and I'm out of ideas. Looked at about 2 dozen stack overflow answers but still don't understand how it works:
I am trying to create a new ClientRepository()
from this composer package.
See example of usage
My php file
<?php
require __DIR__ . '/../vendor/autoload.php';
$clientRepository = new ClientRepository();
I already tried the following:
new League\OAuth2\Server\Repositories\ClientRepository();
new League\OAuth2\Server\ClientRepository();
new League\ClientRepository();
new League\Repositories\ClientRepository();
Structure of composer folders
回答1:
Class ClientRepository is not part of package league/oauth2-server although it's used in some sample code in package documentation. You will need to create it first (by implementing interface \League\OAuth2\Server\Repositories\ClientRepositoryInterface) then use it, which is too complicate for the question.
If you only want to see how Composer works for that package, following piece of code should work:
<?php
require __DIR__ . '/../vendor/autoload.php';
$request = new League\OAuth2\Server\RequestTypes\AuthorizationRequest();
?>
or
<?php
use League\OAuth2\Server\RequestTypes\AuthorizationRequest;
require __DIR__ . '/../vendor/autoload.php';
$request = new AuthorizationRequest();
?>
来源:https://stackoverflow.com/questions/49336656/how-to-autoload-with-composer