如何在laravel中要求和配置依赖项? [英] How to require and configure a dependency in laravel?

查看:58
本文介绍了如何在laravel中要求和配置依赖项?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望将 chargebee php API 添加到我的laravel项目

I wish to add chargebee php api to my laravel project

为此我首先参加了

composer require chargebee/chargebee-php

这将chargebee API添加到了供应商/chargbee

This added the chargebee API to vendor/chargbee

接下来我需要做的是包括并配置api.这可以通过以下方式完成:

Next thing I would need to do is to include and configure the api. This can be done by:

require_once base_path('vendor/chargebee/ChargeBee.php');
ChargeBee_Environment::configure("your_site", "{your_site_api_key}");

我的问题是我应该在哪里做?我希望包含ChargBee.php并将其配置为位于通用位置,以便以后可以在任何Controller中使用它并且已经进行了设置.Laravel中执行此操作的默认方法是什么?

My question is where should i do this? I would like the including of ChargBee.php and configuring it to be in a generic place so i can later use it in any Controller and it would already be set up. What is the default way to do this in Laravel?

推荐答案

您应该设置一个新的服务提供商,如官方文档.

You should set up a new service provider, like described in the official documentation.

提示1:从命令行运行 php artisan make:provider ChargeBeeServiceProvider 来生成新的服务提供商.

Hint 1: Run php artisan make:provider ChargeBeeServiceProvider from command line to generate the new service provider.

提示2:仔细研究 register()方法.您应该在此处注册 ChargeBee 服务",最有可能像单例一样.那是应该放置API配置的地方.

Hint 2: take a closer look at the register() method. You should register the ChargeBee "service" there, most probably like a singleton. That's the place, where the API configuration should be put.

由于在chargebee库中使用了自动加载功能(请参见其 composer.json ),则不需要 require_once base_path('vendor/chargebee/ChargeBee.php'); 行.只需使用类即可.

Since autoload is being used in the chargebee library (see its composer.json), there is no need for the require_once base_path('vendor/chargebee/ChargeBee.php'); line. Just simply use the classes.

这篇关于如何在laravel中要求和配置依赖项?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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