在CodeIgniter中安装全向插件时出错 [英] Error in installing omnipay in CodeIgniter

查看:258
本文介绍了在CodeIgniter中安装全向插件时出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



但我遇到此错误:

 致命错误:未捕获异常'Omnipay\Common\Exception\RuntimeException在C:\ xampp \htdocs\testserver\vendor\omnipay\common\src\Omnipay\Common\中的Class'\Omnipay\PayPal Express\Gateway'not found'消息GatewayFactory.php:105 
堆栈跟踪:#0 [内部函数]: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}抛出在C:\ xampp\htdocs\testserver\vendor\omnipay \common\src\Omnipay\Common\GatewayFactory.php on line 105

I已遵循此帖( CodeIgniter + omnipay安装)的建议,但他们的建议都没有为我工作。



我使用codeigniter 2.2.4和apache 5.4.19



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

解决方案

我想我解决了。
我发现可能会碰到 FCPATH。 'vendor'自动加载和APPPATH。 'core'类自动加载。如果你尝试扩展你的控制器从 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目录。如果您没有其他供应商依赖项,请使用位于 index.php composer.json >包含下一个代码的文件:

  {
require:{
omnipay / omnipay 〜2.0
}
}

strong>导航控制台到您的项目的根文件夹,还包括新创建的json文件。



3。启动命令 composer install



4。在应用程序启动之前添加composer自动加载文件。执行此操作的一种方法是接近 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允许访问); 

使用\Omnipay\Omnipay;
使用\Omnipay\Common\GatewayFactory;

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

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


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?

解决方案

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

这篇关于在CodeIgniter中安装全向插件时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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