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.

tmarois
  • 2,424
  • 2
  • 31
  • 43
Geordy James
  • 2,358
  • 3
  • 25
  • 34

6 Answers6

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

tmarois
  • 2,424
  • 2
  • 31
  • 43
Js_P
  • 76
  • 3
6

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

protected $helpers = ["form"] // BaseController.php:29;
Badiparmagi
  • 1,285
  • 1
  • 14
  • 24
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
Jopie
  • 336
  • 3
  • 9
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)

Abdulla Nilam
  • 36,589
  • 17
  • 64
  • 85
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

Rahma TV
  • 49
  • 4
  • 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. – Chris Nadovich Jul 20 '23 at 17:54
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

b126
  • 544
  • 4
  • 11