Error in installing omnipay in CodeIgniter

后端 未结 1 885
[愿得一人]
[愿得一人] 2021-01-15 21:58

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-wi

相关标签:
1条回答
  • 2021-01-15 22:36

    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);
            }
        }
    
    0 讨论(0)
提交回复
热议问题