在 CodeIgniter 中安装omnipay 时出错CodeIgniter、omnipay

2023-09-06 16:17:07 作者:沉浮于世的微尘__

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 'OmnipayCommonExceptionRuntimeException' with message 'Class 'OmnipayPayPal ExpressGateway' not found' in C:xampphtdocs	estservervendoromnipaycommonsrcOmnipayCommonGatewayFactory.php:105
    Stack trace: #0 [internal function]: OmnipayCommonGatewayFactory->create('PayPal Express')
    #1 C:xampphtdocs	estservervendoromnipaycommonsrcOmnipayOmnipay.php(103): call_user_func_array(Array, Array)
    #2 C:xampphtdocs	estserverapplicationcontrollersTest.php(18): OmnipayOmnipay::__callStatic('create', Array)
    #3 C:xampphtdocs	estserverapplicationcontrollersTest.php(18): OmnipayOmnipay::create('PayPal Express')
    #4 [internal function]: Test->Pay()
    #5 C:xampphtdocs	estserversystemcoreCodeIgniter.php(360): call_user_func_array(Array, Array)
    #6 C:xampphtdocs	estserverindex.php(203): require_once('C:xampphtdocs...')
    #7 {main} thrown in C:xampphtdocs	estservervendoromnipaycommonsrcOmnipayCommonGatewayFactory.php on line 105
cod4安装出错怎么办

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?

解决方案

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 OmnipayOmnipay;
    use OmnipayCommonGatewayFactory;

    class Test extends Back_Controller
    {
        function __construct()
        {
            parent::__construct();
        }

        public function index()
        {
            var_dump(new Omnipay);
            var_dump(new GatewayFactory);
        }
    }