$psr4 and namespace |
Hello,
I am trying to use the SVGGraph library from Goat1000 within CodeIgniter and I encountered a problem with the namespaces used by this library. I use CI 4.4.7 and put the library inside the Libraries folder of my app under app/Libraries/Goat1000/SVGGraph The SVGGraph library is namespaced and has per default at the beginning of each file the line: namespace Goat1000\SVGGraph So, to adapt it to my settings, I added the following element to the $psr4 array in the file app/Config/autoload.php 'Goat1000\\SVGGraph' => APPPATH . 'Libraries\Goat1000\SVGGraph' . However, I get the following error when I try to instantiate the first class (which is the class SVGGraph): Class "App\Libraries\Goat1000\SVGGraph\SVGGraph" not found Clearly, the 'App' namespace doesn't get translated. I can see that this is the problem, since if I change the namespace line into the SVGGraph Class and put explicitely namespace App\Libraries\Goat1000\SVGGraph it works and the class gets loaded. Since the SVGGraph library contains over 170 files, I do not want to edit each file to change the namespace... Therefore my questions: Is this the intended way to use autoload.php? Since I never used namespaces until now I may have misunderstood its function. Is there another way to achieve my goal, i.e. change the namespace for all classes/files? Thank you in advance!! Philippe
Why don't you use Composer to install the library?
It configures autoloader automatically. But your config in app/Config/autoload.php seems valid. PHP Code: 'Goat1000\\SVGGraph' => APPPATH . 'Libraries\Goat1000\SVGGraph' How do you call the libarary?
(03-31-2024, 06:50 PM)kenjis Wrote: Why don't you use Composer to install the library? Thank you for your answer, Kenjis. Sorry, I forgot to mention that I also tried to install the library with Composer, but it didn't make any change to the autoloader, at least I didn't see any change. This is my controller with the method where I call the library (just the example given by the author): Code: <?php
Hi Kenjis,
As I controlled again everything this morning, after posting my reply, I noticed that I had changed something in the "use" list of my Controller. I changed it and now everything works!!! I had problems with cache effects from Firefox yesterday and tried quite a lot of combinations, and it seems that in doing this I just remained stuck into the wrong combination... Sorry about that. Since I couldn't find any description on how to integrate SVGGraph into CodeIgniter, I'll post a short description as soon as I have tested everything. What is the best place to post it?
If you mean in this forums,
https://forum.codeigniter.com/forumdisplay.php?fid=34
Ok, I'll do that
btw. you were right, the code you showed is exactly what I had changed. And yes, the class "Goat1000\SVGGraph\autoloader" really exists. Thank you for your help! PHP Code: // namespace \ back slash path / forward slash What did you Try? What did you Get? What did you Expect?
Joined CodeIgniter Community 2009. ( Skype: insitfx )
I am feeling embarrassed about what I just discovered: Actually it was the composer installation which was working, and the problems I described were caused by my wrong paths in the "use" lines. I said before that the composer installation didn't change anything because I didn't know it has its own autoload file.
Investigating a bit, I saw that Composer just maps all the classes in the file autoload_classmap and I suppose I could do the same in the autoload file from CI. This is not very smart, but it works. I am old fashioned (started programming in 1974) and do not like tools like Composer which does things I do not control, but I suppose I'll have to live with it. Thanks to both of you for your support and please accept my apologies for describing a non-existent problem. (04-01-2024, 06:04 AM)PhilInLoco Wrote: Investigating a bit, I saw that Composer just maps all the classes in the file autoload_classmap and I suppose I could do the same in the autoload file from CI. This is not very smart, but it works. The classmap in autoload_classmap is for optimization by Composer. Because looking for files from the filesystem takes time. It is configured in https://github.com/codeigniter4/CodeIgni...r.json#L83 You can define classmap manually in the CI4 autoloader: https://codeigniter4.github.io/CodeIgnit...l#classmap But if you use Composer, you don't need to use it. |
Welcome Guest, Not a member yet? Register Sign In |