How to autoload with composer?

一笑奈何 提交于 2021-02-10 18:06:03

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!