Laravel 无法找到我的库中的类。

huangapple go评论100阅读模式
英文:

Laravel can't find a class from my library

问题

我正在尝试将OAuth2 Azure身份验证添加到我的Laravel项目中。我找到了这个库:text并按照GitHub上的所有步骤进行了操作。但是它不起作用。当我在我的控制器中重定向到Azure时,当在AzureServiceProvider.php中加载register函数时,会出现错误。Laravel无法在绑定函数中找到类\VinhHoang\OAuth2\Client\Provider\Azure。

我的登录控制器:

  1. namespace App\Http\Controllers;
  2. use Illuminate\Http\Request;
  3. use Illuminate\Foundation\Auth\AuthenticatesUsers;
  4. use Illuminate\Support\Facades\Auth;
  5. use App\Models\User;
  6. use Lang;
  7. use Azure;
  8. class AuthController extends Controller
  9. {
  10. use AuthenticatesUsers;
  11. public function showLoginForm() {
  12. if (Auth::Check())
  13. {
  14. return redirect('/');
  15. }
  16. return Azure::redirect();
  17. }
  18. }

AzureServiceProvider.php:

  1. namespace VinhHoang\OAuth2;
  2. use App;
  3. use Illuminate\Support\ServiceProvider;
  4. class AzureServiceProvider extends ServiceProvider
  5. {
  6. public function boot()
  7. {
  8. $this->publishes([
  9. __DIR__ . '/Config/oauth2azure.php' => config_path('oauth2azure.php')
  10. ]);
  11. }
  12. /**
  13. * Register the application services.
  14. *
  15. * @return void
  16. */
  17. public function register()
  18. {
  19. App::bind('azure', function () {
  20. return new \VinhHoang\OAuth2\Client\Provider\Azure;
  21. });
  22. }
  23. }

Azure.php:

  1. namespace VinhHoang\OAuth2\Client\Provider;
  2. use League\OAuth2\Client\Provider\AbstractProvider;
  3. use League\OAuth2\Client\Provider\Exception\IdentityProviderException;
  4. use League\OAuth2\Client\Tool\BearerAuthorizationTrait;
  5. use League\OAuth2\Client\Grant\AbstractGrant;
  6. use VinhHoang\OAuth2\Client\Grant\JwtBearer;
  7. use VinhHoang\OAuth2\Client\Token\AccessToken;
  8. use Psr\Http\Message\ResponseInterface;
  9. use \Firebase\JWT\JWT;
  10. use Illuminate\Http\RedirectResponse;
  11. class Azure extends AbstractProvider
  12. {
  13. use BearerAuthorizationTrait;
  14. public $urlLogin = "https://login.microsoftonline.com/";
  15. public $pathAuthorize = "/oauth2/authorize";
  16. public $pathToken = "/oauth2/token";
  17. public $scope = [];
  18. public $tenant = "";
  19. public $urlAPI = "https://graph.windows.net/";
  20. public $resource = "";
  21. public $API_VERSION = "1.6";
  22. public $authWithResource = true;
  23. public function __construct(array $collaborators = [])
  24. {
  25. //load config
  26. $config = config('oauth2azure');
  27. $this->tenant = $config['tenant'];
  28. parent::__construct($config, $collaborators);
  29. $this->grantFactory->setGrant('jwt_bearer', new JwtBearer);
  30. }
  31. /**
  32. * Redirect the user of the application to the provider's authentication screen.
  33. *
  34. * @return \Symfony\Component\HttpFoundation\RedirectResponse
  35. */
  36. public function redirect()
  37. {
  38. return new RedirectResponse($this->getAuthorizationUrl());
  39. }
  40. // ...
  41. }

Laravel Framework 6.20.44
PHP 7.4.33

我已经尝试过的方法:

  • composer update
  • composer dump-autoload
  • php artisan cache:clear
  • php artisan optimize
  • 在composer.json中,“vinhhoang/oauth2-azure”: “^1.0”已经存在

但所有这些都没有改变,我认为命名空间也是正确的,包括大小写。有关为什么在AzureServiceProvider.php中加载register函数时,Laravel无法在绑定函数中找到类\VinhHoang\OAuth2\Client\Provider\Azure的任何其他想法吗?

PS:
对于我的英语,我感到抱歉。

英文:

I'm trying to add oauth2 azure authentification to my laravel project. I found this library : text and i've followed all the steps on the git. But this not work. When i redirect to azure in my controller it make an error when register function is load in AzureServiceProvider.php. Laravel could not find class \VinhHoang\OAuth2\Client\Provider\Azure in the bind function.

My login controller :

  1. namespace App\Http\Controllers;
  2. use Illuminate\Http\Request;
  3. use Illuminate\Foundation\Auth\AuthenticatesUsers;
  4. use Illuminate\Support\Facades\Auth;
  5. use App\Models\User;
  6. use Lang;
  7. use Azure;
  8. class AuthController extends Controller
  9. {
  10. use AuthenticatesUsers;
  11. public function showLoginForm() {
  12. if (Auth::Check())
  13. {
  14. return redirect('/');
  15. }
  16. return Azure::redirect();
  17. }
  18. }

AzureServiceProvider.php :

  1. namespace VinhHoang\OAuth2;
  2. use App;
  3. use Illuminate\Support\ServiceProvider;
  4. class AzureServiceProvider extends ServiceProvider
  5. {
  6. public function boot()
  7. {
  8. $this->publishes([
  9. __DIR__ . '/Config/oauth2azure.php' => config_path('oauth2azure.php')
  10. ]);
  11. }
  12. /**
  13. * Register the application services.
  14. *
  15. * @return void
  16. */
  17. public function register()
  18. {
  19. App::bind('azure', function () {
  20. return new \VinhHoang\OAuth2\Client\Provider\Azure;
  21. });
  22. }
  23. }

Azure.php :

  1. namespace VinhHoang\OAuth2\Client\Provider;
  2. use League\OAuth2\Client\Provider\AbstractProvider;
  3. use League\OAuth2\Client\Provider\Exception\IdentityProviderException;
  4. use League\OAuth2\Client\Tool\BearerAuthorizationTrait;
  5. use League\OAuth2\Client\Grant\AbstractGrant;
  6. use VinhHoang\OAuth2\Client\Grant\JwtBearer;
  7. use VinhHoang\OAuth2\Client\Token\AccessToken;
  8. use Psr\Http\Message\ResponseInterface;
  9. use \Firebase\JWT\JWT;
  10. use Illuminate\Http\RedirectResponse;
  11. class Azure extends AbstractProvider
  12. {
  13. use BearerAuthorizationTrait;
  14. public $urlLogin = "https://login.microsoftonline.com/";
  15. public $pathAuthorize = "/oauth2/authorize";
  16. public $pathToken = "/oauth2/token";
  17. public $scope = [];
  18. public $tenant = "";
  19. public $urlAPI = "https://graph.windows.net/";
  20. public $resource = "";
  21. public $API_VERSION = "1.6";
  22. public $authWithResource = true;
  23. public function __construct(array $collaborators = [])
  24. {
  25. //load config
  26. $config = config('oauth2azure');
  27. $this->tenant = $config['tenant'];
  28. parent::__construct($config, $collaborators);
  29. $this->grantFactory->setGrant('jwt_bearer', new JwtBearer);
  30. }
  31. /**
  32. * Redirect the user of the application to the provider's authentication screen.
  33. *
  34. * @return \Symfony\Component\HttpFoundation\RedirectResponse
  35. */
  36. public function redirect()
  37. {
  38. return new RedirectResponse($this->getAuthorizationUrl());
  39. }
  40. ...

Laravel Framework 6.20.44
PHP 7.4.33

What I've already tried :

  • composer update
  • composer dump-autoload
  • php artisan cache:clear
  • php artisan optimize
  • In composer.json, "vinhhoang/oauth2-azure": "^1.0", is present

But all of this change nothing and the namespace are good i think. Lower and Uppercase in namespace are good too.

Any other idea for why when register function is load in AzureServiceProvider.php, Laravel could not find class \VinhHoang\OAuth2\Client\Provider\Azure in the bind function. ?

PS :
Sorry for my english.

答案1

得分: 0

已解决!命名空间错误!我必须从库中更改命名空间,从:"VinhHoang\OAuth2\Client\Provider\Azure" 更改为 "VinhHoang\OAuth2\Provider\Azure"。只需从命名空间中删除 "Client"。对于 vinhhoang 库中的每个类都要这样做。

英文:

Resolved !! The namespace were wrong ! I have to change namespace from library, from : "VinhHoang\OAuth2\Client\Provider\Azure" to VinhHoang\OAuth2\Provider\Azure. Just removed "Client" from namespace. Do that foreach class in vinhhoang library.

huangapple
  • 本文由 发表于 2023年6月15日 15:08:47
  • 转载请务必保留本文链接:https://go.coder-hub.com/76479945.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定