Welcome Guest, Not a member yet? Register   Sign In
Need help in Controller
#1

[eluser]raiser[/eluser]
Dear Friends,

How can i reduce the code in controller? That is, is there any way to include helper files and library files only once? Now i am including individually for each and every function.

So kindly guide me to avoid this.. Here, the below code gives you an idea of what i am following the structure now...

Code:
class Events_awards extends CI_Controller{
        public function _construct()
        {
            parents::_construct();
        }
            
        function index()
        {
            $this->load->helper('html');
            $this->load->helper('url');
            $this->load->library('session');
            $this->load->library('table');
            $this->load->database();
            
            $data['title']    =    "Administrator";
            
            $this->load->view('events-awards-list', $data);
        }
        
        function add_event()
        {
            $this->load->helper('html');
            $this->load->helper('url');
            $this->load->library('session');
            $this->load->helper('form');
            $this->load->helper(array('form', 'url'));
            $this->load->database();
            $data['title']    =    "Administrator";
            
            if(!$this->input->post('submit'))
            {
                $this->load->view('events-awards-add', $data);
            }
            
            if($this->input->post('submit'))
            {    
                $this->load->model('events_awards_model');
                
                $this->events_awards_model->insert_event();
                redirect('events_awards');
            }
        }
    }


Hope you can understand the code.. If any difficulty, let me know..
#2

[eluser]toopay[/eluser]
You can put any helper/library loading statement into your constructor function, or if you use them in any controllers (or most of them) consider to autoload all that stuffs.
#3

[eluser]raiser[/eluser]
Thanks toopay for replying.. Can you explain little bit clearly? I cant understand what you are suggesting.. I am a new bie to this CI..
#4

[eluser]toopay[/eluser]
About loading library and helper at once in constructor, here you can do that...
Code:
class Events_awards extends CI_Controller{
        public function _construct()
        {
            parents::_construct();
            $this->load->helper(array('html', 'url', 'form'));
            $this->load->library(array('session', 'table'));
            $this->load->database();
        }
            
        function index()
        {
            /* Now you can remove this, since we already put that at constructor
            $this->load->helper('html');
            $this->load->helper('url');
            $this->load->library('session');
            $this->load->library('table');
            $this->load->database();
            */

            $data['title']    =    "Administrator";
            
            $this->load->view('events-awards-list', $data);
        }
        
        function add_event()
        {
            /* This not necessary anymore too
            $this->load->helper('html');
            $this->load->helper('url');
            $this->load->library('session');
            $this->load->helper('form');
            $this->load->helper(array('form', 'url'));
            $this->load->database();
            */
            $data['title']    =    "Administrator";
            
            if(!$this->input->post('submit'))
            {
                $this->load->view('events-awards-add', $data);
            }
            
            if($this->input->post('submit'))
            {    
                $this->load->model('events_awards_model');
                
                $this->events_awards_model->insert_event();
                redirect('events_awards');
            }
        }
    }
About autoloading your resources, read about that at : http://ellislab.com/codeigniter/user-gui...oader.html
#5

[eluser]raiser[/eluser]
Thanks toopay.. It is working now.. I defined all files in autoload.php..




Theme © iAndrew 2016 - Forum software by © MyBB