CodeIgniter Forums
Accessing PHP library in CI4 - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: CodeIgniter 4 (https://forum.codeigniter.com/forumdisplay.php?fid=28)
+--- Forum: CodeIgniter 4 Support (https://forum.codeigniter.com/forumdisplay.php?fid=30)
+--- Thread: Accessing PHP library in CI4 (/showthread.php?tid=77862)



Accessing PHP library in CI4 - Hexes - 10-27-2020

Hello,

I am rewriting my old CI3 project to CI4.

I have a part where the app accesses the external PHP library and uses its functions.

it looks like this:

  
PHP Code:
        require_once('libwebtopay/WebToPay.php');

        try {
            $request WebToPay::redirectToPayment(array( 

But in CI4, I get an error. 

Class 'App\Controllers\WebToPay' not found

It looks like CI4 thinks that WebToPay is a controller.

Is it possible to make CI4 read external PHP classes?



RE: Accessing PHP library in CI4 - paulbalandan - 10-27-2020

prepend the namespace separator to WebToPay.

\WebToPay::redirect


RE: Accessing PHP library in CI4 - Hexes - 10-28-2020

Thank you, it worked!

Now looks like it gets to the library.

I don't know if this error is familiar with the previous one.

Any idea of how to solve it?

[Image: Screenshot-2020-10-28-at-11-07-50.png]


RE: Accessing PHP library in CI4 - captain-sensible - 10-28-2020

this looks like its about namespaces by the way i came across a nifty check using spark

cd to root of app and run from command line:
Code:
php spark namespaces

thats tells you what namespaces can be found.

on the

Code:
require_once('libwebtopay/WebToPay.php');

CI4 uses as far as i can see 2 ways to find classes, if you use composer to get software it automatically loads if not you add entries to app/Config/Autoload.php

Now i use PHPmailer and had a similar problem. First i told CI4 where to find classes via $psr4 in Autoload, then there are several classes so i listed them all in the controller:


use PHPMailer\PHPMailer\OAuth;
use PHPMailer\PHPMailer\Exception;
use ..

you see that Exception class for PHPMailer, i think you have an exception class in your webpay which it needs to have access to:

Do you have a link to the webpay so i can have a look ?

Also always interested in 3rd party stuff i might be able to use!


RE: Accessing PHP library in CI4 - Hexes - 10-28-2020

Thank you for your answer! It really helped me a lot.

This is how I solved my problem:

I just added those classes into Autoload $classmap:

it looks like this now:



PHP Code:
    public $classmap = [
        
'WebToPay'   => 'libwebtopay/WebToPay.php',
        
'WebToPayException'   => 'libwebtopay/WebToPay.php',
    ]; 

Let me know if it's ok to use it this way.

But at least it works fine now.


RE: Accessing PHP library in CI4 - captain-sensible - 10-28-2020

Good news :^),

i believe its legit and another way of doing it and faster than namespace approach according to notes in autoload class .


RE: Accessing PHP library in CI4 - paulbalandan - 10-28-2020

I see the WebToPay lib is not using namespaces. In that case, your classmap approach is preferred.q