CodeIgniter Forums
$psr4 Namespace Paths - Printable Version

+- CodeIgniter Forums (
+-- Forum: CodeIgniter 4 (
+--- Forum: CodeIgniter 4 Support (
+--- Thread: $psr4 Namespace Paths (/thread-75586.html)

$psr4 Namespace Paths - Gary - 02-24-2020

Ok, this one is bending my noobie-brain…

I’ve been experimenting with the $psr4 namespace, and, for demonstrating my problem, have the following (in Autoload.php):

$psr4 = [
          'Config'     => APPPATH . 'Config',
          'App'        => APPPATH,
          'Libraries'  => APPPATH . 'Libraries',

So one may be mistaken to believe that Libraries would be mapped (in the namespace and also in the file structure on the drive) as a sub-directory of the APPPATH (\app)…but if I use it as I’d have expected:

use Libraries\Functions;

// or, in case anyone thinks it’s now being referenced relative to \Config, the same happens with:
use \Libraries\Functions;

I get:  “Error: Class 'Libraries\Functions' not found”.

If, however, I precede the reference to use with “\App”, as in:

use \App\Libraries\Functions;

everything works.

So the $psr4 definition doesn’t seem to be doing anything… indeed, if I remove ‘Libraries’ from Autoload.php, it makes no difference, and everything works without it.

What am I missing?



RE: $psr4 Namespace Paths - InsiteFX - 02-24-2020

That's because all the folders under app belong to the App namespace.

RE: $psr4 Namespace Paths - captain-sensible - 02-24-2020

i've also been playing with namespace to use things like PHPmailer and my own classes. All i an tell you is what works for me.

So this is my dev structure in a directory called CI located at : /var/www/htdocs

bash-5.0$ tree -f -L 1
├── ./
├── ./
├── ./DCO.txt
├── ./PHPMailer
├── ./
├── ./
├── ./Vagrantfile.dist
├── ./admin
├── ./app
├── ./composer.json
├── ./composer.lock
├── ./contributing
├── ./env
├── ./fontawesome
├── ./forms
├── ./gulpfile.js
├── ./license.txt
├── ./node_modules
├── ./package-lock.json
├── ./package.json
├── ./phpunit.xml.dist
├── ./public
├── ./scss
├── ./spark
├── ./stale.yml
├── ./system
├── ./tests
├── ./user_guide_src
└── ./writable

to put it simply PHPMailer is at same level of app

the name space of PHPMailer classes including Exception, SMTP , POP is:
namespace PHPMailer\PHPMailer;

to use PHPMailer from a controller i listed it in app/config/autoload $psrf = [
section as:

'PHPMailer\\PHPMailer'=> ROOTPATH.'PHPMailer/src'

then in a controller called sendmail i used:
<?php namespace App\Controllers;

use CodeIgniter\Controller;
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\OAuth;
use PHPMailer\PHPMailer\Exception;
use PHPMailer\PHPMailer\SMTP;

then further down :

$mail = new PHPMailer();
$mail->Timeout = 20;
$mail->SMTPDebug = 0;
$mail->Host = '';

it successfully sent me an email from a form that submits to controller via POST

so as insiteFX just said everything under app uses ,APP namespace but if you follow my approach you will see that I put PHPMailer outside app and it definitely takes notice .

RE: $psr4 Namespace Paths - Gary - 04-13-2020

Thanks for the explanation and working example.

What was bending my brain at the time was the fact that I had one 3rd party addon (in \app\ThirdParty\ZxcvbnPhp) added and working perfectly out-the-box without the slightest glitch or problem, but when I later experimented with Myth/Auth, I battled to get it working.

If I have another try with it, or when I have problems with the next 3rd-party addon, I'll try it the way you suggest.