CodeIgniter Forums

Full Version: how to load the helper(url) automatically in CI4?
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Pages: 1 2
I want to use url function in global, 
helper('url');
it is very easy in CI3, but I don't know how to load it automatically in CI4
Simplest method currently is to have a BaseController that loads it up. It can be done something like:

Code:
class BaseController extends CodeIgniter\Controller
{
   protected $helpers = [];  // this is already part of the Controller class

   public function __construct(...$params)
   {
       parent::__construct(...$params);

       $this->helpers = array_merge($this->helpers, ['url', 'foo']);
   }
}

This is a little more work up front, but most projects will see you using a base controller or two anyway.
it doesn't work. I want to use site_url(''), in all the Controllers and views.thanks
Load the helper on BaseController, like this:

Code:
class BaseController extends CodeIgniter\Controller
{
  public function __construct(...$params)
  {
      parent::__construct(...$params);

      helper(['url','form']);
  }
}

Other controllers should extend this.
(11-10-2016, 06:48 PM)ridho Wrote: [ -> ]Load the helper on BaseController, like this:

Code:
class BaseController extends CodeIgniter\Controller
{
  public function __construct(...$params)
  {
      parent::__construct(...$params);

      helper(['url','form']);
  }
}

Other controllers should extend this.

Thanks a lot, it work well now...
Doh! Sorry about that. In my example I was adding the helpers to the helpers array AFTER calling the parent constructor, when it would have needed to happen BEFORE calling the parent constructor.

Either way, ridho's solution is a bit simpler and works great, also!
(11-09-2016, 08:12 PM)kilishan Wrote: [ -> ]Simplest method currently is to have a BaseController that loads it up. It can be done something like:

Code:
class BaseController extends CodeIgniter\Controller
{
   protected $helpers = [];  // this is already part of the Controller class

   public function __construct(...$params)
   {
       parent::__construct(...$params);

       $this->helpers = array_merge($this->helpers, ['url', 'foo']);
   }
}

This is a little more work up front, but most projects will see you using a base controller or two anyway.

I thing:


Code:
class BaseController extends CodeIgniter\Controller
{
    protected $helpers = [];  // this is already part of the Controller class

    public function __construct(...$params)
    {
        $this->helpers[] = 'url';
        parent::__construct(...$params);
    }
}
@HTLove 
good method too!!
(11-09-2016, 08:12 PM)kilishan Wrote: [ -> ]Simplest method currently is to have a BaseController that loads it up. It can be done something like:

Code:
class BaseController extends CodeIgniter\Controller
{
   protected $helpers = [];  // this is already part of the Controller class

   public function __construct(...$params)
   {
       parent::__construct(...$params);

       $this->helpers = array_merge($this->helpers, ['url', 'foo']);
   }
}

This is a little more work up front, but most projects will see you using a base controller or two anyway.

Does it possible to load helper function through config/Autoload.php?
Pages: 1 2