How to use composer packages in codeigniter?

后端 未结 7 1576
逝去的感伤
逝去的感伤 2020-12-02 17:38

I\'ve followed this article: http://philsturgeon.co.uk/blog/2012/05/composer-with-codeigniter

But I get Fatal error: Class \'Buzz\\Browser\' not found.<

相关标签:
7条回答
  • 2020-12-02 18:09

    For CodeIgniter 3.x and composer, it's suggested to just set $config['composer_autoload'] to TRUE or a custom path in application/config/config.php.

    It seems that CI assumes the vendor directory is within the application directory. That wasn't my case. I did the following:

    $config['composer_autoload'] = 'vendor/autoload.php';

    0 讨论(0)
  • 2020-12-02 18:11

    You can add directly the Composer Autoloder in your controller:

    // Composer Autoloader
    require FCPATH.'vendor/autoload.php';
    
    0 讨论(0)
  • 2020-12-02 18:12

    There are two ways you can autoload the class file which is required using composer.

    1. Add below line in index.php in the root directory.

      require FCPATH . 'vendor/autoload.php';
      
    2. Or autoload directly in the controller where you want to use.

      defined('BASEPATH') OR exit('No direct script access allowed');
      
      require FCPATH . 'vendor/autoload.php';
      
      class Home extends CI_Controller {...}
      
    0 讨论(0)
  • 2020-12-02 18:14

    Credit to @jmadsen

    This is possible by just getting the order of loading correct:

    /*
     * --------------------------------------------------------------------
     * LOAD THE BOOTSTRAP FILE
     * --------------------------------------------------------------------
     *
     * And away we go...
     *
     */
    // Composer Autoloader
    require FCPATH . 'vendor/autoload.php';
    
    require_once BASEPATH.'core/CodeIgniter.php';
    
    /* End of file index.php */
    
    0 讨论(0)
  • 2020-12-02 18:14

    EDIT: Damn, I just said pretty much exactly the same thing as @Tjorriemorrie

    If you've followed all the other directions correctly, all you need to do is add the following code near the very the end your index.php file:

    /*
     * --------------------------------------------------------------------
     * COMPOSER AUTOLOAD
     * --------------------------------------------------------------------
     */
    include_once './vendor/autoload.php';
    

    ...just make sure you slot it in before the CodeIgniter Bootstrap file is called:

    /*
     * --------------------------------------------------------------------
     * LOAD THE BOOTSTRAP FILE
     * --------------------------------------------------------------------
     *
     * And away we go...
     *
     */
     require_once BASEPATH.'core/CodeIgniter.php';
    
    0 讨论(0)
  • 2020-12-02 18:25

    I'm using Kenjis codeigniter composer package, and it puts the vendor directory off of the root. Since there is no predefined constant (that I know of) for the root, I used the following:

    $root = getcwd();
    $config['composer_autoload'] = "$root/vendor/autoload.php";
    
    0 讨论(0)
提交回复
热议问题