• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
launch helpers with extern method

#1
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
Reply

#2
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'); 
Reply


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


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