CodeIgniter Forums
how to load the helper(url) automatically in CI4? - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: CodeIgniter 4 (https://forum.codeigniter.com/forum-28.html)
+--- Forum: CodeIgniter 4 Discussion (https://forum.codeigniter.com/forum-31.html)
+--- Thread: how to load the helper(url) automatically in CI4? (/thread-66591.html)

Pages: 1 2


how to load the helper(url) automatically in CI4? - startbbs - 11-09-2016

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


RE: how to load the helper(url) automatically in CI4? - ciadmin - 11-09-2016

Did you read the CI4 user guide?
https://bcit-ci.github.io/CodeIgniter4/helpers/url_helper.html#loading-this-helper


RE: how to load the helper(url) automatically in CI4? - kilishan - 11-09-2016

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.


RE: how to load the helper(url) automatically in CI4? - startbbs - 11-10-2016

it doesn't work. I want to use site_url(''), in all the Controllers and views.thanks


RE: how to load the helper(url) automatically in CI4? - ridho - 11-10-2016

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.


RE: how to load the helper(url) automatically in CI4? - startbbs - 11-10-2016

(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...


RE: how to load the helper(url) automatically in CI4? - kilishan - 11-10-2016

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!


RE: how to load the helper(url) automatically in CI4? - HTLove - 11-10-2016

(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);
    }
}



RE: how to load the helper(url) automatically in CI4? - startbbs - 11-10-2016

@HTLove 
good method too!!


RE: how to load the helper(url) automatically in CI4? - startbbs - 11-10-2016

(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?