CodeIgniter Forums
$psr4 and namespace - 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: $psr4 and namespace (/showthread.php?tid=90524)

Pages: 1 2


$psr4 and namespace - PhilInLoco - 03-31-2024

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


RE: $psr4 and namespace - kenjis - 03-31-2024

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?


RE: $psr4 and namespace - PhilInLoco - 04-01-2024

(03-31-2024, 06:50 PM)kenjis Wrote: 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?

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
namespace App\Controllers;
use App\Controllers\BaseController;
use CodeIgniter\Shield\Auth;
use App\Libraries\Goat1000\SVGGraph\autoloader;
use App\Libraries\Goat1000\SVGGraph\SVGGraph;

class Catalogue extends BaseController {
...

public function makeGraph() {
$graph = new SVGGraph(640, 480);
$graph->colours(['red','green','blue']);
$graph->values(100, 200, 150);
$graph->links('/Tom/', '/Dick/', '/Harry/');
$graph->render('BarGraph')
}



RE: $psr4 and namespace - kenjis - 04-01-2024

(04-01-2024, 02:00 AM)PhilInLoco Wrote:
Code:
use App\Libraries\Goat1000\SVGGraph\autoloader;
use App\Libraries\Goat1000\SVGGraph\SVGGraph;

The namespace is incorrect.

Code:
use Goat1000\SVGGraph\SVGGraph;

The class "Goat1000\SVGGraph\autoloader" really exists?


RE: $psr4 and namespace - PhilInLoco - 04-01-2024

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?


RE: $psr4 and namespace - kenjis - 04-01-2024

If you mean in this forums,
https://forum.codeigniter.com/forumdisplay.php?fid=34


RE: $psr4 and namespace - PhilInLoco - 04-01-2024

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!


RE: $psr4 and namespace - InsiteFX - 04-01-2024

PHP Code:
// namespace \ back slash            path / forward slash
'InsiteFX\Admin'        => ROOTPATH 'InsiteFX/Admin',          // InsiteFX/Admin Module 



RE: $psr4 and namespace - PhilInLoco - 04-01-2024

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.


RE: $psr4 and namespace - kenjis - 04-01-2024

(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/CodeIgniter4/blob/c6b75055f8283630aa472c4554e0910af7b29a1d/composer.json#L83

You can define classmap manually in the CI4 autoloader:
https://codeigniter4.github.io/CodeIgniter4/concepts/autoloader.html#classmap
But if you use Composer, you don't need to use it.