How to load module helper in CodeIgniter 4?

How to load module helper in CodeIgniter 4?

Created:06 Oct 2021 23:09:12 , in  Web development

A few words on how to load a custom helper function defined in a Codeigniter 4 module.

Suppose you have a bunch of modules living in modules directory of your Codeigniter 4 project. The modules directory is registered in App/Config/Autoload.php using $psr4 property as follows:


'Modules'     => ROOTPATH. 'modules',

Or in other words, ROOTPATH . 'modules' directory is mapped to namespace Modules.

Also, let's assume that the module is called ImageUpload and it has a director called Helpers in it.

In the helpers directory there is a file called imageupload_helper.php which contains function listImages. You want to load and then use this function in some other module, like Blog module, which also lives in modules directory.

The helper imageupload can be loaded in a view of the Blog module as follows:


<?php helper(['\Modules\ImageUpload\Helpers\imageupload']); ?>

Notice, it is not just the filename, but the namespace that you need to pass to the helper().

Secondly, while loading a helper file , you omit _helper.php bit of its name (Helpers have _helper.php suffix in CodeIgniter4 as mentioned above for imageupload_helper.php).

Once the helper is loaded, listImages() is available for use in the view.

This post was updated on 12 Oct 2021 00:45:09

Tags:  CodeIgniter 


Author, Copyright and citation

Author

Sylwester Wojnowski

Author of the this article - Sylwester Wojnowski - is a sWWW web developer. He has been writing computer code for the websites and web applications since 1998.

Copyrights

©Copyright, 2024 Sylwester Wojnowski. This article may not be reproduced or published as a whole or in parts without permission from the author. If you share it, please give author credit and do not remove embedded links.

Computer code, if present in the article, is excluded from the above and licensed under GPLv3.

Citation

Cite this article as:

Wojnowski, Sylwester. "How to load module helper in CodeIgniter 4?." From sWWW - Code For The Web . https://swww.com.pl//main/index/how-to-load-module-helper-in-codeigniter-4

Add Comment

Allowed BB Code - style tags: [b][/b], [i][/i], [code=text][/code],[code=javascript][/code],[code=php][/code],[code=bash][/code],[code=css][/code],[code=html][/code]


I constent to processing my data given through this form for purposes of a reply by the administrator of this website.

Recent Comments

Nobody has commented on this post yet. Be first!