将CHARGEBEE库集成到LARAVEL [英] Integrate CHARGEBEE library to LARAVEL

查看:89
本文介绍了将CHARGEBEE库集成到LARAVEL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我开始使用LARAVEL最新版本7.12,尝试集成 CHARGEBEE 库以向 Chargebee api 发出请求时遇到问题.

我读到可以用composer安装软件包,我做到了:

composer require chargebee/chargebee-php:'>=2, <3'

现在,我已经在这里下载 chargebee lib :/vendor/chargebee/chargebee-php/

现在我也看到了堆栈溢出问题在这里:

为了使用户正确使用此库包,我需要创建一个ServiceProvider,所以我这样做了:

 php artisan make:provider ChargeBeeServiceProvider

然后我真的不知道如何在这里编写REGISTER()函数,我还添加了以下行:App\Providers\ChargebeeServiceProvider::class, to /config/app.php to 'providers'

ChargebeeServiceProvider

现在我有一个控制器:/app/http/controllers/PortalController,我正在尝试使用它:

ChargeBee_Environment::configure("sitename","apikeyvalue"); 

$all = ChargeBee_Customer::all(array( "firstName[is]" => "John", 
"lastName[is]" => "Doe", "email[is]" => "john@test.com" ));

foreach($all as $entry){
  $customer = $entry->customer();
  $card = $entry->card(); 
}

PortalController

但是在前端,这给了我一个错误:

找不到错误类'App \ Http \ Controllers \ ChargeBee_Customer'

不太确定如何在Laravel上使用此自定义 Chargebee 库.

有人可以帮助以正确的方式进行整合吗?

谢谢!

解决方案

两件事,您可以在Laravel中使用任何第三方库作为服务提供者(DI),也可以在需要的地方直接使用它(主要是在控制器中使用)

不仅第三方库,您还可以在Service Provider中简化类等的实例创建.基本上,它是用于依赖项注入的,一般来说,检查 IoC 即可.

您尝试/混合了两种情况,没有完全使用任何一种方法.

  1. 您已经创建了一个名为ChargeBeeServiceProvider的服务提供程序,但是它没有返回/绑定任何资源,也没有在register方法中完成任何API初始化.在此注册并使用DI.您也可以绑定属性.这是官方文档.

  2. 您尝试通过初始化ChargeBee_Environment并直接使用ChargeBee_Customer直接使用,但是您没有提到在哪里寻找此类,这是您收到错误的原因(错误类'App \ Http \ Controllers \ ChargeBee_Customer'找不到).我的意思是编译器将在相同的本地空间中查找ChargeBee_Environment,但ChargeBee_Environment可从全局范围中找到.

相同代码的简单解决方案.将\添加到类中以从全局范围查看或使用文件顶部的use语句类似于在Java中导入,但仍然需要在php中包含该文件,但不要担心,chargebee利用自动加载器(请在此处检查-> composer.json ),因此会同时以两种方式自动加载.

\ChargeBee_Environment::configure("sitename","apikeyvalue"); 

$all = \ChargeBee_Customer::all(array( "firstName[is]" => "John", 
"lastName[is]" => "Doe", "email[is]" => "john@test.com" ));

尝试上面的方法,让我知道.从移动设备回答.代码未经过全面测试(只需复制您的代码并将其添加到\中).

请原谅:)

Im starting using LARAVEL last version 7.12 and I'm having an issue trying to integrate CHARGEBEE library to make request to Chargebee api.

I was reading that I can install packages with composer, I did it:

composer require chargebee/chargebee-php:'>=2, <3'

Doing that now I have downloaded chargebee lib here: /vendor/chargebee/chargebee-php/

Now also I saw This stack overflow question here:

That for the user to correctly use this Library-Package I need to create a ServiceProvider so I did:

 php artisan make:provider ChargeBeeServiceProvider

then I really don't know how to write the REGISTER() function here, I added also this line: App\Providers\ChargebeeServiceProvider::class, to /config/app.php to 'providers'

ChargebeeServiceProvider

Right now I have a controller: /app/http/controllers/PortalController and I'm trying to use this:

ChargeBee_Environment::configure("sitename","apikeyvalue"); 

$all = ChargeBee_Customer::all(array( "firstName[is]" => "John", 
"lastName[is]" => "Doe", "email[is]" => "john@test.com" ));

foreach($all as $entry){
  $customer = $entry->customer();
  $card = $entry->card(); 
}

PortalController

BUT on the frontend it's giving me an error:

Error Class 'App\Http\Controllers\ChargeBee_Customer' not found

Not really sure how i can use this custom Chargebee library here on Laravel.

can somebody please help to integrate in the correct way ?

Thanks!

解决方案

Two things, you can use any third party library as Service Provider(DI) in Laravel or use it directly where ever you needed(mostly in controller)

Not just third party library you can ease the instance creation of class etc in Service Provider. Basically it's for dependency injection, Check IoC in general you'll understand it better.

You tried/mixed both in your case and didn't completely use any single approach.

  1. You've created a Service Provider called ChargeBeeServiceProvider but it doesn't return/bind any resource or no API initialization done in register method. This is where you register and make use of DI. You can bind in properties as well. Here is the official doc to it.

  2. You tried to use directly by initializing ChargeBee_Environment and use ChargeBee_Customer but you didn't mention where to look for this class that is the reason you get the error(Error Class 'App\Http\Controllers\ChargeBee_Customer' not found). I mean compiler will look for ChargeBee_Environment from the same local space but ChargeBee_Environment is available from global scope.

Simple solution to your same code. Add \ to the class to look from global scope or make use of use statement in the top of your file similar to import in java but still we need to require/include the file in php but don't worry, chargebee make use of autoloader(Check here -> composer.json) so it will be loaded automatically in both approach.

\ChargeBee_Environment::configure("sitename","apikeyvalue"); 

$all = \ChargeBee_Customer::all(array( "firstName[is]" => "John", 
"lastName[is]" => "Doe", "email[is]" => "john@test.com" ));

Try the above and let me know. Answered from mobile device. Code is not tested fully(Just copied your and added \ to it).

Excuse brevity :)

这篇关于将CHARGEBEE库集成到LARAVEL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆