$psr4 Namespace Paths |
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): Code: $psr4 = [ 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: Code: use Libraries\Functions; I get: “Error: Class 'Libraries\Functions' not found”. If, however, I precede the reference to use with “\App”, as in: Code: 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? Thanks. Gary
That's because all the folders under app belong to the App namespace.
What did you Try? What did you Get? What did you Expect?
Joined CodeIgniter Community 2009. ( Skype: insitfx )
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 . ├── ./CODE_OF_CONDUCT.md ├── ./CONTRIBUTING.md ├── ./DCO.txt ├── ./PHPMailer ├── ./PULL_REQUEST_TEMPLATE.md ├── ./README.md ├── ./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: Code: <?php namespace App\Controllers; 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 .
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. Thanks. |
Welcome Guest, Not a member yet? Register Sign In |