Use third party library globally in all controllers

Hello,

I use PhpOffice librariy and declare it in many controllers:

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

Nevertheless, when I declare it only in my AppController.php, the library is not loaded in respective controllers and I get error:

Class ‘App\Controller\Spreadsheet’ not found

Is it possible to declare a third party library globally in CakPhp?

Thank you very much!

What you are referring to are the namespaces attached to the library which is default PHP (nothing directly associated to CakePHP)

I won’t explain how namespaces work but to answer your question: You “could” technically create a class_alias but I really don’t recommend that.

I would rather recommend you watch my talk about re-using code and namepsaces from last years Cakefest: CakeFest Virtual 2021 Day 2 - How to re use code: Utility Classes and PHP Namespaces - Kevin Pfeifer - YouTube

It is normal to reference each class with their respective namespace inside each PHP class so I would recommend you try to get used to it.

Thank you very much for the link to cakefest video.