• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
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.
Support Development  • Practical CodeIgniter 3  •
Myth:AuthVulcan - CLI Tools for CI4
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.
Need a free blog? try Mitha Blog. Here You can create a free website or build a blog with dozens of free, customizable, mobile-ready designs and themes.
Reply

#6
(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!
Support Development  • Practical CodeIgniter 3  •
Myth:AuthVulcan - CLI Tools for CI4
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


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2019 MyBB Group.