Welcome Guest, Not a member yet? Register   Sign In
Accessing PHP library in CI4


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:

        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?

prepend the namespace separator to WebToPay.


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]

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:
php spark namespaces

thats tells you what namespaces can be found.

on the


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!

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.

Good news :^),

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

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

Theme © iAndrew 2016 - Forum software by © MyBB