Welcome Guest, Not a member yet? Register   Sign In
Modules in CI4 - Helpers and Constants
#1

Hi All,
I am currently working on a project, where I am keeping all my code in a module. So my structure is as below:
Code:
-app
-mymodule
  -admin
    -Config
      -Constants.php
    -Helpers
      -functions_helper.php
  -frontend
-public
-vendor
.... (other standard folders)

Now everything else from Modules (Routes, Controllers, Views) is working fine as I have provided namespace in Autoload.php, but Helpers and Constants are not working as they are not classes. 
In app > Config > autoload.php, I tried to add files as well with correct path, but it is still not working.
Code:
public $files = [
        '/mymodule/admin/Helpers/functions_helper.php'
    ];

I can make it work if I make them classes, but before I do that way, I want to check how to make it work without making classes. 
I read CI guide at https://codeigniter4.github.io/userguide...ml#helpers , where it is written that "Helpers will be automatically discovered within defined namespaces when using the 
Code:
helper() 
function, as long as it is within the namespaces [b]Helpers[/b] directory:"
But it is not working. I tried that way as well in the Controller.

Code:
helper('MyModule\Admin\Helpers\functions');

Any idea from anyone?
Reply
#2

What version of CodeIgniter are you running?

You should not be using a slash \ at the begining of the path.
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply
#3

PHP Code:
public $files = [
      ROOTPATH 'mymodule/admin/Helpers/functions_helper.php',
    ]; 

Or update CI and use:
Code:
public $helpers = [];
Reply
#4

Thanks everyone! It is now working with below both options:

ROOTPATH . 'mymodule/admin/Helpers/*.php',

and '../mymodule/admin/Helpers/*.php'

For some reason I believe that I used ROOTPATH earlier but it was not working. May be I used /mymodule at that time (can't remember).
Reply




Theme © iAndrew 2016 - Forum software by © MyBB