在CodeIgniter中安装omnipay时出错

时间:2015-09-06 13:48:07

标签: php codeigniter composer-php omnipay

我正在尝试在CodeIgniter(版本2.2.4)中添加omnipay我按照安装作曲家的说明使用此链接:https://philsturgeon.uk/blog/2012/05/composer-with-codeigniter/

但是我遇到了这个错误:

    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

我已经按照这篇文章中提出的建议(CodeIgniter + omnipay installation),但他们的建议都没有对我有用。

我正在使用codeigniter 2.2.4和apache 5.4.19

任何人都可以帮我解决这个问题吗?

1 个答案:

答案 0 :(得分:2)

我想我解决了。 我发现可能存在FCPATH . 'vendor'自动加载和APPPATH的冲突。 '核心'类自动加载。如果您尝试从CI_MY_前缀核心类扩展您的控制器,我相信它会起作用。另一方面,如果您尝试使用不以CI_MY_开头的核心类或您配置的任何内容,则无法从供应商目录中找到所需的类。

我玩了一遍,发现如果你改变配置文件中用于自动加载核心类的代码就可以了。 你可以用

function __autoload($class)
{
    if(strpos($class, 'CI_') !== 0)
    {
        include_once( APPPATH . 'core/'. $class . EXT );
    }
}

function __autoload($class) {
    if (substr($class,0,3) !== 'CI_') {
        if (file_exists($file = APPPATH . 'core/' . $class . EXT)) {
            include $file;
        }
    }
}

我换了这个文件:

spl_autoload_register(function ($class) {
    if (substr($class,0,3) !== 'CI_') {
        if (file_exists($file = APPPATH . 'core/' . $class . EXT)) {
            include $file;
        }
    }
});

刚刚测试过,它正在运行。

以下是一步一步满足这些需求的所有流程:

1. 将omnipay下载到root / vandor目录。如果您没有其他供应商依赖项,请使用位于composer.json文件旁边的新创建的index.php文件执行此操作,并使用下一个代码:

    {
        "require": {
            "omnipay/omnipay": "~2.0"
        }
    }

2. 将控制台导航到项目的根文件夹,该文件夹还包含新创建的json文件。

3。启动命令composer install

4. 在应用程序启动前包含composer autoload文件。实现此目的的一种方法是在index.php文件的末尾,只需

    require_once BASEPATH.'core/CodeIgniter.php';

,下一个代码:

require_once __DIR__.'/vendor/autoload.php';

5. APPPATH . 'config/config.php'文件的末尾,将此代码段也用于使用核心类:

    spl_autoload_register(function ($class) {
        if (substr($class,0,3) !== 'CI_') {
            if (file_exists($file = APPPATH . 'core/' . $class . EXT)) {
                include $file;
            }
        }
    });

6。在定义类之前的文件开头的控制器中,使用所需的供应商类:

    <?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);
        }
    }