首页 > 解决方案 > 将作曲家添加到现有的 codeigniter 项目

问题描述

我想将 Composer 添加到我的 CodeIgniter 项目并自动加载依赖项。我遵循了几个步骤,但我可能遗漏了一些东西。这是我遵循的步骤。

$config['composer_autoload']在我更改为的 config.php 文件中TRUE,我也尝试将其更改为FCPATH.'vendor\autoload.php'不起作用。

在项目根文件夹中,我使用了这个命令:composer require mpdf/mpdf它创建了一个带有 mpdf 的供应商文件夹。

读了一点之后,我在 index.php 的末尾做了这个更改:

/*
 * --------------------------------------------------------------------
 * LOAD THE BOOTSTRAP FILE
 * --------------------------------------------------------------------
 *
 * And away we go...
 */
include_once './vendor/autoload.php';
require_once BASEPATH.'core/CodeIgniter.php';

这是更改后的项目结构:

项目结构图

这是composer.json:

{
        "description": "The CodeIgniter framework",
        "name": "codeigniter/framework",
        "type": "project",
        "homepage": "https://codeigniter.com",
        "license": "MIT",
        "support": {
            "forum": "http://forum.codeigniter.com/",
            "wiki": "https://github.com/bcit-ci/CodeIgniter/wiki",
            "slack": "https://codeigniterchat.slack.com",
            "source": "https://github.com/bcit-ci/CodeIgniter"
        },
        "require": {
            "php": ">=5.3.7",
            "mpdf/mpdf": "^8.0"
        },
        "suggest": {
            "paragonie/random_compat": "Provides better randomness in PHP 5.x"
        },
        "require-dev": {
            "mikey179/vfsStream": "1.1.*",
            "phpunit/phpunit": "4.* || 5.*"
        }
    }

这是我用来测试 mpdf 的控制器:

    <?php
defined('BASEPATH') OR exit('No direct script access allowed');

class TestingGround extends CI_Controller{
    public function index() {
        $this->load->view("testing_ground");
    }
    public function pdf(){

      $mpdf = new mPDF();

      // Write some HTML code:

      $mpdf->WriteHTML('Hello World');

      // Output a PDF file directly to the browser
      $mpdf->Output();

      }
}

这是我得到的错误:Class 'mPDF' not found C:\wamp64\www\kariyer_1.6\application\controllers\TestingGround.php 12

标签: phpcodeignitercomposer-php

解决方案


您必须使用正确的类 -mPDF使用命名空间,并且如https://mpdf.github.io/installation-setup/installation-v7-x.html中所述,您必须将其实例化如下:

$mpdf = new \Mpdf\Mpdf();

推荐阅读