Welcome Guest, Not a member yet? Register   Sign In
Repetive code in controller
#1

[eluser]Unknown[/eluser]
Code:
<?php

class User extends Controller
{
    function __construct()
    {
        parent::Controller();
    }
    
    public function login()
    {
        $data['title'] = build_title('Log In');
        
        $this->user->login();
        
        $this->load->view('section/global/html', $data);
        $this->load->view('section/global/header', $data);
        $this->load->view('section/global/top', $data);
        $this->load->view('user/login', $data);
        $this->load->view('section/global/footer', $data);
    }
    
    public function logout()
    {
        $data['title'] = build_title('Log Out');
        
        $this->user->logout();
        
        $this->load->view('section/global/html', $data);
        $this->load->view('section/global/header', $data);
        $this->load->view('section/global/top', $data);
        $this->load->view('user/logout', $data);
        $this->load->view('section/global/footer', $data);
    }
    
    public function signup()
    {
        $data['title'] = build_title('Sign Up');
        
        $this->load->view('section/global/html', $data);
        $this->load->view('section/global/header', $data);
        $this->load->view('section/global/top', $data);
        $this->load->view('user/logout', $data);
        $this->load->view('section/global/footer', $data);
    }
}

?>

Alright, so take a look at the code above. You can see that I have three functions inside of the controller class. The problem is that there is a lot of repetitive code inside of each function. For each, I have to define the variables in $data, then load each of the views individually. Each view represents a section of the final page that will be sent to the browser - the start HTML, header, top navigation, then the actual page content followed by a footer.

Is there a way to make the code less repetitive? I don't want to have to write the same code every time for each controller function I have. Most of the view loads ($this->load->view) are the same for every single function since every page will follow a similar format. Also is there a way to automatically load multiple views at once?

I was thinking of something like this:

Code:
function start($data)
{
                $this->load->view('section/global/html', $data);
        $this->load->view('section/global/header', $data);
        $this->load->view('section/global/top', $data);
}

function end($data)
{
                $this->load->view('section/global/footer', $data);
}

function data() { ? }


class User extends Controller
{
     ....

    
     function whatever()
     {
           $data = data();
           start($data); $this->load->view('user/page', $data); end($data);
     }
}


I would appreciate any help. Thanks.
#2

[eluser]brianw1975[/eluser]
extend Controller... as MY_Controller.php place it in libraries (but there is no need to add it to the autoloader.php file

place all repetitive code/functions in MY_Controller.php and then in your controllers instead of "class something extends Controller" do "class something extends MY_Controller"

heading to bed now, but if you do a search in the forums for "extend controller", google for "extend CodeIgniter Controller" and check in the wiki you'll get loads of answers, check the "Approaches" in the wiki as well, someone may have added something for this in there as well.
#3

[eluser]alboyd[/eluser]
Creating a BaseController is often a useful thing but in this particular case I think you need a view_loader view or something.
FOR the login view:
Code:
$data['master_view'] = 'user/login';
$this->load->view('view_loader', $data);

Then in your view_loader view you have this:
Code:
$this->load->view('section/global/html');
        $this->load->view('section/global/header');
        $this->load->view('section/global/top');
        $this->load->view($master_view, $data);
        $this->load->view('section/global/footer');

EDIT: you don't need $data in the $master_view load either I don't think...




Theme © iAndrew 2016 - Forum software by © MyBB