Welcome Guest, Not a member yet? Register   Sign In
Repeating Code in Controller
#1

[eluser]Unknown[/eluser]
Hello all,

My problem is I'm finding that I'm repeating my code in the controllers and was wondering if there was a better way of handling it? In the below code I have index and settings_profile functions... within those I have several arrays that do the same thing. For example, I have this being called twice. I tried putting the array in the __construct but that didn't work. Any help would be great. Thanks.

Code:
$memProfile = $this->account_details_model->get_member_profile();
                $data['member_info'] = array('member_name' => $memProfile->first_name);

Code:
class App extends Controller {


       function __construct() {

                parent::Controller();
    }
    
    function index() {
        
        $is_logged_in = $this->session->userdata('is_logged_in');
                if (!isset($is_logged_in) || $is_logged_in != true) {
                        redirect('../site/login');

                } else {
                $memProfile = $this->account_details_model->get_member_profile();
                $data['member_info'] = array('member_name' => $memProfile->first_name);

                $data['page'] = array('Dash' => 'current', 'BB' => '', 'BB1' => '', 'BB2' => '', 'BB3' => '', 'BB4' => '', 'CL' => '', 'settings' => '', 'profile' => '' );
                $data['main_content'] = 'app/dashboard_view';
                $this->load->view('app_template/template', $data);

                }
    
    }

       function settings_profile(){
                $memProfile = $this->account_details_model->get_member_profile();
                $data['member_info'] = array('member_name' => $memProfile->first_name);
                
                $data['page'] = array('Dash' => '', 'BB' => '', 'BB1' => '', 'BB2' => '', 'BB3' => '', 'BB4' => '', 'CL' => '', 'settings' => 'current', 'profile' => 'current' );
                $data['main_content'] = 'app/settings_profile_view';
                $this->load->view('app_template/template', $data);
          
       }

}
#2

[eluser]frist44[/eluser]
Private functions:

Code:
private function _get_member_profile() {
    return $this->account_details_model->get_member_profile();
}

Then call this function from your controller functions.

$memProfile = $this->_get_member_profile();
#3

[eluser]InsiteFX[/eluser]
Use a MY_Controller it's in the users guide.

Enjoy
InsiteFX




Theme © iAndrew 2016 - Forum software by © MyBB