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

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

Laravel can't find a class from my library

问题

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

我的登录控制器:

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Foundation\Auth\AuthenticatesUsers;
use Illuminate\Support\Facades\Auth;
use App\Models\User;
use Lang;
use Azure;

class AuthController extends Controller
{
    use AuthenticatesUsers;

    public function showLoginForm() {
      if (Auth::Check())
      {
        return redirect('/');
      }
      return Azure::redirect();
    }
}

AzureServiceProvider.php:

namespace VinhHoang\OAuth2;

use App;
use Illuminate\Support\ServiceProvider;

class AzureServiceProvider extends ServiceProvider
{

    public function boot()
    {
        $this->publishes([
            __DIR__ . '/Config/oauth2azure.php' => config_path('oauth2azure.php')
        ]);
    }

    /**
     * Register the application services.
     *
     * @return void
     */
    public function register()
    {
        App::bind('azure', function () {
            return new \VinhHoang\OAuth2\Client\Provider\Azure;
        });
    }
}

Azure.php:

namespace VinhHoang\OAuth2\Client\Provider;

use League\OAuth2\Client\Provider\AbstractProvider;
use League\OAuth2\Client\Provider\Exception\IdentityProviderException;
use League\OAuth2\Client\Tool\BearerAuthorizationTrait;
use League\OAuth2\Client\Grant\AbstractGrant;
use VinhHoang\OAuth2\Client\Grant\JwtBearer;
use VinhHoang\OAuth2\Client\Token\AccessToken;
use Psr\Http\Message\ResponseInterface;
use \Firebase\JWT\JWT;
use Illuminate\Http\RedirectResponse;

class Azure extends AbstractProvider
{
    use BearerAuthorizationTrait;

    public $urlLogin = "https://login.microsoftonline.com/";
    public $pathAuthorize = "/oauth2/authorize";
    public $pathToken = "/oauth2/token";

    public $scope = [];

    public $tenant = "";

    public $urlAPI = "https://graph.windows.net/";
    public $resource = "";

    public $API_VERSION = "1.6";

    public $authWithResource = true;

    public function __construct(array $collaborators = [])
    {
        //load config
        $config = config('oauth2azure');
        $this->tenant = $config['tenant'];

        parent::__construct($config, $collaborators);
        $this->grantFactory->setGrant('jwt_bearer', new JwtBearer);
    }

    /**
     * Redirect the user of the application to the provider's authentication screen.
     *
     * @return \Symfony\Component\HttpFoundation\RedirectResponse
     */
    public function redirect()
    {
        return new RedirectResponse($this->getAuthorizationUrl());
    }
    // ...
}

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 :

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Foundation\Auth\AuthenticatesUsers;
use Illuminate\Support\Facades\Auth;
use App\Models\User;
use Lang;
use Azure;


class AuthController extends Controller
{
    use AuthenticatesUsers;


    public function showLoginForm() {
      if (Auth::Check())
      {
        return redirect('/');
      }
      return Azure::redirect();
    }
}

AzureServiceProvider.php :

namespace VinhHoang\OAuth2;

use App;
use Illuminate\Support\ServiceProvider;


class AzureServiceProvider extends ServiceProvider
{

    public function boot()
    {
        $this->publishes([
            __DIR__ . '/Config/oauth2azure.php' => config_path('oauth2azure.php')
        ]);
    }

    /**
     * Register the application services.
     *
     * @return void
     */
    public function register()
    {
        App::bind('azure', function () {
            return new \VinhHoang\OAuth2\Client\Provider\Azure;
        });
    }
}

Azure.php :

namespace VinhHoang\OAuth2\Client\Provider;

use League\OAuth2\Client\Provider\AbstractProvider;
use League\OAuth2\Client\Provider\Exception\IdentityProviderException;
use League\OAuth2\Client\Tool\BearerAuthorizationTrait;
use League\OAuth2\Client\Grant\AbstractGrant;
use VinhHoang\OAuth2\Client\Grant\JwtBearer;
use VinhHoang\OAuth2\Client\Token\AccessToken;
use Psr\Http\Message\ResponseInterface;
use \Firebase\JWT\JWT;
use Illuminate\Http\RedirectResponse;

class Azure extends AbstractProvider
{
    use BearerAuthorizationTrait;

    public $urlLogin = "https://login.microsoftonline.com/";
    public $pathAuthorize = "/oauth2/authorize";
    public $pathToken = "/oauth2/token";

    public $scope = [];

    public $tenant = "";

    public $urlAPI = "https://graph.windows.net/";
    public $resource = "";

    public $API_VERSION = "1.6";

    public $authWithResource = true;

    public function __construct(array $collaborators = [])
    {
        //load config
        $config = config('oauth2azure');
        $this->tenant = $config['tenant'];

        parent::__construct($config, $collaborators);
        $this->grantFactory->setGrant('jwt_bearer', new JwtBearer);
    }

    /**
     * Redirect the user of the application to the provider's authentication screen.
     *
     * @return \Symfony\Component\HttpFoundation\RedirectResponse
     */
    public function redirect()
    {
        return new RedirectResponse($this->getAuthorizationUrl());
    }

 ...

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:

确定