How to autoload helper functions in codeigniter 4
Asked Answered
L

6

10

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.

Letitialetizia answered 21/1, 2017 at 8:30 Comment(4)
too early to use CI 4Washboard
@AbdullaNilam yes I know it sir. I am just trying the upcoming version to understand its new features not for doing any projects on it as they don't recommend Cl 4. Since Cl 4 is not yet officially released I don't get any help from 3 party sites.Letitialetizia
just i have to say wait for it. :)Washboard
@AbdullaNilam oke sir..Letitialetizia
M
6

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

Midwife answered 1/2, 2017 at 18:49 Comment(0)
L
6

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

protected $helpers = ["form"] // BaseController.php:29;
Locus answered 18/4, 2020 at 15:30 Comment(1)
How to load library ?Paper
A
3

Just add name of helper(s) you want to load in

protected $helpers = [] in

/app/Controllers/BaseController.php

CodeIgniter4 will load those helpers automatically.

For example:

class BaseController extends Controller
{
    /**
     * An array of helpers to be loaded automatically upon
     * class instantiation. These helpers will be available
     * to all other controllers that extend BaseController.
     *
     * @var array
     */
    protected $helpers = ['cookie','date']; // <=== this
Armes answered 8/5, 2021 at 14:38 Comment(0)
W
2

For Future Reference

Since there is no autoload in autoload.php you have to load all helper files by itself. Unlike global load in CodeIgniter 3.

How to load

public function FunctionName()
{
    helper(['form', 'validation']); # before retun to view.
    #rest of your code
}

Do we need to load this into each and every method(s)?

Yes. Since I play around it I'm unable to load helper file globally.(2018-2-07)

Washboard answered 7/2, 2018 at 9:59 Comment(1)
Now we can load the helpers globally via BaseController.phpPutup
B
2

Step 1

Create helper on App/Helpers. Example : alert_helper.php, MUST ending with _helper

enter image description here

enter image description here

Step 2

Load helper on Constructor or Method. helper(['alert']);

enter image description here

Step 3

Use helper

enter image description here

DONE

Buncombe answered 22/9, 2020 at 20:17 Comment(1)
Thanks so much for the comment that helpers need to be named with the _helper suffix. I had a bug caused by me not knowing this. Thanks again.Polynomial
N
1

UPDATE CodeIgniter 4.3+

CodeIgniter 4.3.0 has finally added this feature. Just edit the app/Config/Autoload.php file and add the helpers you need globally.

public $helpers = ['repository_helper', 'serializer_helper'];

https://codeigniter.com/user_guide/general/helpers.html#auto-loading-helpers

Neiman answered 20/2, 2023 at 16:53 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.