codeigniter4 / CodeIgniter4

Open Source PHP Framework (originally from EllisLab)
https://codeigniter.com/
MIT License
5.35k stars 1.9k forks source link

Loading namespace helpers doesn't work as expected CI4 alpha5 #1726

Closed d4mn closed 5 years ago

d4mn commented 5 years ago

name: Bug report about: Help us improve the framework by reporting bugs!


Describe the bug Loading helpers from a namespace doesnt work because to the path always is prepended 'Helpers/' $paths = $loader->search('Helpers/' . $filename);

CodeIgniter 4 version Codeigniter 4 alpha 5

Affected module(s) Common.php function function helper($filenames)

Expected behavior, and steps to reproduce if appropriate If i understand and its tricky if namespaced helper then 'Helpers/' should be prepended before helper name ex. i want to load helper('\Modules\Blog\blog') blog_helper.php then the path should be /Modules/Blog/Helpers/blog_helper.php but the search path is: Helpers/\Modules\Blog\blog_helper.php

lonnieezell commented 5 years ago

By design, it looks in the Helpers folder, so there's no bug there. That's exactly as described in the user guide.

If you have this structure:

Modules/
    Blog/
        Helpers/
            blog_helper.php

Then you would use helper('Modules\Blog\blog_helper). You do need to make sure that in app/Config/Autoload.php that a namespace is setup for not just Modules but each module in there, so Modules\Blog.

Additionally, just calling helper('blog') should work also since all namespaces are scanned by default to locate a helper file with that name.

Are you adding a namespace to the helper file itself? In that case you'd have to call the functions with a namespace, which gets ugly.

d4mn commented 5 years ago

Hi, I can confirm that by calling helper('blog') is working when a namespace Modules\Blog is added in Autoload.php. My functions in helper file doesn't have a namespace. But when trying to include helper file with a namespace helper('Modules\Blog\blog) the path of the file will be Helpers/Modules\Blog\blog and then file locator will search for that path in all namespaces so i think there is a bug when including helpers with a namespace but it needs more confirmation.

Thanx in advance.

lonnieezell commented 5 years ago

You're right. Fixing now.