phpspreadsheetcodeigniter-4

Implement PhpSpreadsheet in Codeigniter 4


i am really a beginner and i am trying to install PhpSpreadsheet in Codeigniter 4 in the ThirdParty folder. The instructions I find going around the net refer to "composer" but I have no idea how to run this command (I'm in Windows 10, XAMPP, Codeigniter 4.1.1 and PHP 7.3). I would still like to install everything manually.

So I proceeded like this:

public $psr4 = [

APP_NAMESPACE => APPPATH, // For custom app namespace

'Config'      => APPPATH . 'Config',

'PhpOffice\PhpSpreadsheet' => APPPATH . 'ThirdParty/PhpOffice/PhpSpreadsheet', 

];

Then I created the Example.php controller like so:

<? php
namespace App\Controllers;

use \PhpOffice\PhpSpreadsheet\Spreadsheet;
use \PhpOffice\PhpSpreadsheet\Writer\Xlsx;

class Example extends BaseController
{
    public function index ()
    {
        $ spreadsheet = new Spreadsheet ();
    }
}

Well this line of code alone generates the following error:

Error

Interface 'Psr \ SimpleCache \ CacheInterface' not found

APPPATH \ ThirdParty \ PhpOffice \ PhpSpreadsheet \ Collection \ Memory.php at line 13

I have no idea how to fix. Thanks in advance to all those who will answer


Solution

  • Php office and any other libraries have some dependencies (libraries that are needed in the main library).So we usually use package managers like Composer (in PHP) for handling stuff like that. In this case, Phpoffice tells you Psr\SimpleCache\CacheInterface is a required package for running some scripts.

    Installing/using Composer package is very easy on all platforms. After installing you just need to include "vendor/autoload.php" file in your code and use every installed package.

    Also use $config['composer_autoload'] = APPATH . 'vendor/'; for setting your composer vendor directory for autoloading by codeigniter itself.

    For adding phpoffice/phpspreadsheet you just need to run command below and all dependencies will install automatically for you.

    composer require phpoffice/phpspreadsheet
    

    (vendor is the directory that composer packages will install on it.)