Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Autoloading not functional with custom namespaces outside application dir #529

Closed
timothymarois opened this issue Jun 1, 2017 · 4 comments

Comments

@timothymarois
Copy link
Contributor

timothymarois commented Jun 1, 2017

This could not be an issue at all, or that I am missing something so trivial and that I am sorry, but from looking at both of these pages, I am unable to replicate the functionality desired here.

https://bcit-ci.github.io/CodeIgniter4/general/modules.html
http://blog.newmythmedia.com/blog/show/2016-03-15_Modules_in_CodeIgniter_4

Added in application/Config/Autoload.php

public $psr4 = [
  'Modules' => ROOTPATH.'modules'
];

Directory exist above the application directory called "modules"

File exists under:
modules/Test/Config/Routes.php

Adding this to the end of the file of application/Config/Routes.php

$files = service('locator')->search('Config/Routes.php');

print_r($files);

foreach ($files as $file)
{
    include_once $file;
}

The print returns array of the application and system Routes.php files, and completely ignored my custom modules/Test/Config/Routes.php

Array ( [0] => ~/application/Config/Routes.php [1] => ~/system/Config/Routes.php )

So from my current tests, I am unable to get custom namespaces (directories) working from the autoload class. Basically its unrecognizable by the psr4 system. So that must be something it can not find within my custom directories?

If anyone can point out a mistake or lead me in the right direction that would be great, otherwise from both of the pages listed above, following the step by step instructions didn't seem to work.

@lonnieezell
Copy link
Member

First, thanks for bringing this up over here. Much easier than Disqus comments. :)

And I just noticed something as I read over the email. I believe what's currently needed is to add a new namespace for each module, like:

public $psr4 = [
  'Test' => ROOTPATH.'modules/Test',
  'Another' => ROOTPATH.'modules/Another',
];

IIRC, the file locater simply looks for the exact folder match, it doesn't do any scanning of sub-folders for possible matches.

This has the benefit, though, that your classes would have a better namespacing (Test instead of Modules\Test).

Let me know if that works for you. If not, I'll try to look into it tonight.

@timothymarois
Copy link
Contributor Author

timothymarois commented Jun 1, 2017

Yeah I thought of that too within my tests, and changing too
'Test' => ROOTPATH.'modules/Test'
Still shows the same results.

Would the modules/Test/Config/Routes.php require something that would cause it to ignore? but it does look like it's unable to locate the file location itself.

Maybe something to add with the debug system, "location failed" so its not a silent result.

@timothymarois
Copy link
Contributor Author

Ok this has actually been resolved. Thanks for your help. You did provide with the solution on the directories. However, What I did notice is I also had to move the 'Test' => ROOTPATH.'modules/Test' from the public class variable to the function scope variable of $psr4. For whatever reason, that did not merge, so obviously it was never looked at. All I did was move the exact array element into the one inside of __construct and it worked.

@lonnieezell
Copy link
Member

Oh, gotcha. Glad you got it working.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants