Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to autoload helper functions in codeigniter 4

I just downloaded CodeIgniter 4 from their official GitHub. They changed a lot from CodeIgniter 3. I want to use base_url() function in the view and for that, you need to load URL helper and in CodeIgniter 3 I autoloaded it in config/autoload.php file. But now they have entirely changed the structure of config/autoload.php file in CodeIgniter 4 and it is very confusing to me.

You can still use the base_url() function in your views in CodeIgniter 4 by using below code in your constructor of controller helper('url');

If anybody who used CodeIgnter 4 knows how to autoload helper functions like url by modifying config/autoload.php file please help me.

like image 611
Geordy James Avatar asked Jan 21 '17 08:01

Geordy James


2 Answers

Add your helper to the array in BaseController.php file like this:

protected $helpers = ["form"] // BaseController.php:29;
like image 115
Badiparmagi Avatar answered Sep 17 '22 08:09

Badiparmagi


CodeIgnter 4 is currently in developing phase so many features are still not available. The answer is you cannot autoload helpers or libraries in autoload.php file in codeigniter 4.

I know this is a feature used by many of us but autoloading every thing will decrease site performance so may be developer team decided to drop this feature.

from Codeigniter 4 Documentation:

You can load your helpers in your controller constructor so that they become available automatically in any function, or you can load a helper in a specific function that needs it.

https://bcit-ci.github.io/CodeIgniter4/general/helpers.html

like image 22
Js_P Avatar answered Sep 18 '22 08:09

Js_P