问题
I'm trying to add omnipay in CodeIgniter (version 2.2.4) I followed the instructions in installing composer using this link:https://philsturgeon.uk/blog/2012/05/composer-with-codeigniter/
but I'm having this error:
Fatal error: Uncaught exception 'Omnipay\Common\Exception\RuntimeException' with message 'Class '\Omnipay\PayPal Express\Gateway' not found' in C:\xampp\htdocs\testserver\vendor\omnipay\common\src\Omnipay\Common\GatewayFactory.php:105
Stack trace: #0 [internal function]: Omnipay\Common\GatewayFactory->create('PayPal Express')
#1 C:\xampp\htdocs\testserver\vendor\omnipay\common\src\Omnipay\Omnipay.php(103): call_user_func_array(Array, Array)
#2 C:\xampp\htdocs\testserver\application\controllers\Test.php(18): Omnipay\Omnipay::__callStatic('create', Array)
#3 C:\xampp\htdocs\testserver\application\controllers\Test.php(18): Omnipay\Omnipay::create('PayPal Express')
#4 [internal function]: Test->Pay()
#5 C:\xampp\htdocs\testserver\system\core\CodeIgniter.php(360): call_user_func_array(Array, Array)
#6 C:\xampp\htdocs\testserver\index.php(203): require_once('C:\xampp\htdocs...')
#7 {main} thrown in C:\xampp\htdocs\testserver\vendor\omnipay\common\src\Omnipay\Common\GatewayFactory.php on line 105
I already followed the suggestions from this post(CodeIgniter + omnipay installation) but none of their suggestion is working for me.
I'm using codeigniter 2.2.4 and apache 5.4.19
Can anyone help me solve this?
回答1:
I think I solved it.
I have found that there could be collision of FCPATH . 'vendor'
autoloading and APPPATH . 'core' class autoloading. If you try to extend your controller from CI_
or MY_
prefixed core class I am sure that would work. In other hand, if you try to use core class that doesn't start with CI_
or MY_
or whatever you configured you couldn't find wanted class from vendor direcory.
I played around and found that if you would change the code used in config file for autoloading core classes it works. You could use
function __autoload($class)
{
if(strpos($class, 'CI_') !== 0)
{
include_once( APPPATH . 'core/'. $class . EXT );
}
}
or
function __autoload($class) {
if (substr($class,0,3) !== 'CI_') {
if (file_exists($file = APPPATH . 'core/' . $class . EXT)) {
include $file;
}
}
}
I swapped that file for this one:
spl_autoload_register(function ($class) {
if (substr($class,0,3) !== 'CI_') {
if (file_exists($file = APPPATH . 'core/' . $class . EXT)) {
include $file;
}
}
});
Just tested and it is working.
Here is all process for those needs it step by step:
1. Download omnipay to root/vandor directory. If you don't have other vendor dependencies, do this with newly created composer.json
file located next to index.php
file with next code:
{
"require": {
"omnipay/omnipay": "~2.0"
}
}
2. Navigate console to root folder of your project that also include newly created json file.
3. Start command composer install
4. Include composer autoload file before application boot. One way for doing this is near the end of the index.php
file, just before line
require_once BASEPATH.'core/CodeIgniter.php';
, put next code:
require_once __DIR__.'/vendor/autoload.php';
5. At the end of APPPATH . 'config/config.php'
file, put this snippet to use core classes as well:
spl_autoload_register(function ($class) {
if (substr($class,0,3) !== 'CI_') {
if (file_exists($file = APPPATH . 'core/' . $class . EXT)) {
include $file;
}
}
});
6. In your controller at the beginning of the file before class is defined use needed vendor classes:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
use \Omnipay\Omnipay;
use \Omnipay\Common\GatewayFactory;
class Test extends Back_Controller
{
function __construct()
{
parent::__construct();
}
public function index()
{
var_dump(new Omnipay);
var_dump(new GatewayFactory);
}
}
来源:https://stackoverflow.com/questions/32424255/error-in-installing-omnipay-in-codeigniter