Geordy James
Geordy James

Reputation: 2408

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.

Upvotes: 10

Views: 23458

Answers (6)

b126
b126

Reputation: 1254

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

Upvotes: 1

Jopie
Jopie

Reputation: 332

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

Upvotes: 3

rahmap
rahmap

Reputation: 57

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

Upvotes: 2

Badiparmagi
Badiparmagi

Reputation: 1285

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

protected $helpers = ["form"] // BaseController.php:29;

Upvotes: 6

Abdulla Nilam
Abdulla Nilam

Reputation: 38672

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)

Upvotes: 2

Js_P
Js_P

Reputation: 76

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

Upvotes: 6

Related Questions