Welcome Guest, Not a member yet? Register   Sign In
how to load the helper(url) automatically in CI4?
#1

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
Q&A for Codeigniter
StartBBS--open-source, light weight forum software.
Best VPS:Digital Ocean
Reply
#2

Did you read the CI4 user guide?
https://bcit-ci.github.io/CodeIgniter4/h...his-helper
Reply
#3

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.
Reply
#4

it doesn't work. I want to use site_url(''), in all the Controllers and views.thanks
Q&A for Codeigniter
StartBBS--open-source, light weight forum software.
Best VPS:Digital Ocean
Reply
#5

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.
Tafsir NU adalah sebuah aplikasi yang berisi kumpulan kajian kitab kuning yang disampaikan oleh Kiai Nahdhotul Ulama seperti Gus Baha, gus mus, dan kiai lainnya dalam format audio.
Reply
#6

(This post was last modified: 11-10-2016, 07:19 PM by startbbs.)

(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...
Q&A for Codeigniter
StartBBS--open-source, light weight forum software.
Best VPS:Digital Ocean
Reply
#7

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!
Reply
#8

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

@HTLove 
good method too!!
Q&A for Codeigniter
StartBBS--open-source, light weight forum software.
Best VPS:Digital Ocean
Reply
#10

(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?
Q&A for Codeigniter
StartBBS--open-source, light weight forum software.
Best VPS:Digital Ocean
Reply




Theme © iAndrew 2016 - Forum software by © MyBB