PHP Requests Library within Codeigniter

前端 未结 5 2446
孤独总比滥情好
孤独总比滥情好 2021-02-20 06:52

I\'m using the requests library (http://requests.ryanmccue.info/) for PHP.

I installed composer and added the following Json configuration in composer.json

5条回答
  •  陌清茗
    陌清茗 (楼主)
    2021-02-20 07:19

    The asker doesn't say what version of Codeigniter they are using, but for 3.x I don't think the accepted answer is the correct way to do it. CI Autoloading documentation here: https://www.codeigniter.com/user_guide/general/autoloader.html

    In 3.x, first you want to turn on Composer autoloading in the application/config/config.php file:

    $config['composer_autoload'] = true;
    

    Note this comment in config.php, it may be very important. More on this below.

    // Enabling this setting will tell CodeIgniter to look for a Composer
    // package auto-loader script in application/vendor/autoload.php.
    

    Then if you have this in your composer.json file:

    "require": {
        "rmccue/requests": ">=1.0"
    
    },
    "autoload": {
            "psr-0": {"Requests": "library/"}
    }
    

    (there is probably a lot more stuff in your file, I edited it out for brevity)

    Once you run composer update, you should end up with Requests working via Composer autoloading, so this should work without the need to specifically load vendor/autoload.php, which you want to avoid.

    Requests::register_autoloader();
    

    NOTE:

    I have multiple installs of Codigniter on my server, and I followed the instructions as prescribed. In my case the vendor directory and composer.json were installed at the same level as the application folder, not in the application folder, therefore enabling Composer autoloading didn't work. If this happens to you, you have 2 choices:

    1. In config.php instead of setting $config['composer_autoload'] = TRUE, you can set it to the path where the vendor file is located: $config['composer_autoload'] = '/path/to/vendor/autoload.php'
    2. Move the composer.json file to the application folder and then run composer update.

    Anyway, hope this helps anyone who might get stuck with this - I burned a good 2 hours trying to resolve the issue before I finally realized that my vendor directory wasn't where CI was looking for it.

提交回复
热议问题