CodeIgniter Forums
launch helpers with extern method - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Using CodeIgniter (https://forum.codeigniter.com/forumdisplay.php?fid=5)
+--- Forum: General Help (https://forum.codeigniter.com/forumdisplay.php?fid=24)
+--- Thread: launch helpers with extern method (/showthread.php?tid=74048)



launch helpers with extern method - arenholf - 07-12-2019

hi everybody !

I'm new in PHP and codeIgniter and i have an issue with my code.
I'm trying to clean my view method by stocking some helpers in extern method.
So, instead to have:


    public function index(){
  
        $this->load->helper('url');
        $this->load->helper('form');
        $this->load->model('Users_model');
       
        $data['records'] = $this->Users_model->getData();

        $this->load->view('templates/header');
        $this->load->view('users', $data);
        $this->load->view('templates/footer');
}

i want to do this:

 public function index(){
  
        loaderMethod();
       
        $data['records'] = $this->Users_model->getData();

        $this->load->view('templates/header');
        $this->load->view('users', $data);
        $this->load->view('templates/footer');
}

    public function loaderMethod(){
        $load = array(
            "$this->load->helper('url');",
            "$this->load->helper('form');",
            "$this->load->model('Users_model');"
        );
        return $load;
    }

i know that stocking these ligns in an array it's a bad idea, but it was for show you that i'm trying to do.
If u have some ideas to give, it will be a pleasure ! Big Grin


RE: launch helpers with extern method - dave friend - 07-12-2019

If you use those same helpers for every controller then consider using Auto-loading Resources. Documentation Here.

It's done using the file application/config/autoload.php. In that file use the following for the values to the $autoload array items you will find in the file.

PHP Code:
$autoload['helper'] = array('url''form');
$autoload['model'] = array('Users_model');