CodeIgniter Forums
How to autoload helper functions in codeigniter 4 - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: CodeIgniter 4 (https://forum.codeigniter.com/forumdisplay.php?fid=28)
+--- Forum: CodeIgniter 4 Support (https://forum.codeigniter.com/forumdisplay.php?fid=30)
+--- Thread: How to autoload helper functions in codeigniter 4 (/showthread.php?tid=67149)



How to autoload helper functions in codeigniter 4 - geordy - 01-21-2017

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 
Code:
helper('url');

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


RE: How to autoload helper functions in codeigniter 4 - enlivenapp - 01-21-2017

From the current CI4 docs it doesn't look like you can autoload helpers like in past versions, though I've not really dug into CI4 yet.

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


RE: How to autoload helper functions in codeigniter 4 - InsiteFX - 01-21-2017

You can if you have a BaseController like the MY_Contoller in CI 3.


RE: How to autoload helper functions in codeigniter 4 - kilishan - 01-22-2017

In the Controller docs it shows that you can use the helpers class property to have it loaded on a per-controller basis. If you want something loaded on every request, currently your best option is to use a base controller like InsiteFX mentioned. Though, you'd want to be careful you didn't overwrite any per-controller helpers:

Code:
class BaseController extends \CodeIgniter\Controller
{
    protected $helpers = [];

    public function __construct()
    {
        $this->helpers = array_merge($this->helpers, ['url', 'form']);
    }
}

class UserController extends BaseController
{
    protected $helpers = ['filesystem', 'number'];
}

Or, even simpler, just load the helper in the BaseController constructor:

Code:
class BaseController extends \CodeIgniter\Controller
{
    protected $helpers = [];

    public function __construct()
    {
        helper(['url', 'form']);
    }
}