Codeigniter 2.0 third_party folder
What is this the third_party folder in codeig开发者_如何学Goniter 2.0 and how to use that?
Packages are new to CI2.0 that allow for the easy distribution of complete sets of resources in a single directory
, containing models, libraries, helpers etc...but not to be confused with modules, as Phil Sturgeon points out quite helpfully.
$this->load->add_package_path()
See the docs for more
If your "third party library code" requires a single file to be "loaded" in order for you to make full use of the library then it should go in your libraries directory. If your "third party library code" is a folder full of files, many of which may be "loaded" and made use of, then it should go in your third_party directory.
That's my interpretation of the CodeIgniter docs for the Loader Class found here: https://www.codeigniter.com/user_guide/libraries/loader.html
Is my interpretation incorrect? If so LMK!
The current convention is to use a folder called "vendor". "third_party" is not commonly used anymore.
As John explained in his answer, a single-file library can be loaded by placing it in the libraries folder:
$this->load->library('my_app');
Multiple single-file libraries can be loaded similarly with an array:
$this->load->library(['email', 'table']);
However, if you have a folder full of classes to be loaded not related to CodeIgniter, you can still place them in the third_party folder, but do not use $this->load->add_package_path()
(it expects a CI structure inside the new third_party package, i.e. with models, configs, helpers, etc.).
I leave an example, Stripe API has a lib folder and an init.php, so I did the following:
- updated init.php references from
dirname(__FILE__) . '/lib/
todirname(__FILE__) . '/
) and placed the file in the third_party/stripe folder together with all contents of the lib folder; - Create, for example, a libraries/Stripe.php;
This would be its constructor:
public function __construct() { $this->CI =& get_instance(); require_once(APPPATH.'third_party/stripe/init.php'); \Stripe\Stripe::setApiKey($this->CI->config->item('stripe_key_secret')); if(ENVIRONMENT === 'development'){ \Stripe\Stripe::setVerifySslCerts(false); } }
Add to it functions that invoke directly Stripe classes, simple e.g.:
public static function create_customer($email, $token) { $stripe_result = FALSE; try { $stripe_result = \Stripe\Customer::create([ 'email' => $email, 'description' => "Customer for $email", 'source' => $token // obtained with Stripe.js ]); } catch(\Stripe\Error\Card $e) { return 'Error ' . $e->getHttpStatus() . ': '. $e->stripeCode . ' -> '. var_export($e->getJsonBody()['error'], TRUE); } catch (Exception $e) { return 'Something else happened, completely unrelated to Stripe -> '. var_export($e, TRUE); } return $stripe_result;
load the library on one of your controllers and invoke the method:
$this->load->library('stripe'); Stripe::create_customer($someones_email, $someones_token); // you can use the result
Just also leaving the updated URL: CodeIgniter's Loader Class documentation.
精彩评论