首页 > 解决方案 > 如何在 codeigniter 中包含供应商自动加载?

问题描述

我是 CodeIgniter 的新手,我只是想问一下如何在 CodeIgniter 中包含供应商/自动加载文件?我还没有尝试任何解决方案,所以也许有人可以帮助我解决这个问题?

标签: codeigniter

解决方案


  1. 在您的配置文件中将composer 自动加载设置false更改为true

$config['composer_autoload'] = TRUE;

  1. 将此行添加到您的index.php文件中。

include_once './vendor/autoload.php';

  1. 在库文件夹中创建库文件作为VendorLibrary.php

代码

use Vendor\ClassName;

class ClassNameLibrary {

    public $class;

    public function __construct()
    {
        $this->class = new ClassName();
    } 

    public function clear($data)
    {
        return $this->class->clean($data);
    }
}

4.像这样在控制器中加载库。

代码

class HomeController extends CI_Controller {

    public function __construct()
    {
        $this->load->library('classnamelibrary');
    } 

    public function index()
    {
        $clean = $this->classnamelibrary->clear($data);
    }
}

推荐阅读