Undermine2k
Undermine2k

Reputation: 1491

Creating a proper controller

I have a controller that that will have many functions, I notice that my get_participants function does not run, when I load my welcome view, because when I try to use <?php echo $product ?> I get undefined variable exception.

I do not wish to have everything in my index function that loads the welcome view, but many functions that create one view. What is the proper way to invoke this controller so that it runs every function inside the class, or is there some better way I should be doing this?

class Welcome extends CI_Controller
{
    function __construct()
    {
        parent::__construct();

        $this->load->helper('url');
        $this->load->library('tank_auth_groups','','tank_auth');
        $this->load->model('Participant_model');
    }

    function index()
    {
        if (!$this->tank_auth->is_logged_in()) {
            redirect('/auth/login/');
        } else {
            $data['user_id']    = $this->tank_auth->get_user_id();
            $data['username']   = $this->tank_auth->get_username();
                $this->load->view('welcome', $data);
        }

    }


   public function get_participants()
    {

        $data['product'] = $this->Participant_model->get_all_records();

        $this->load->view('welcome', $data);
    }

}

View

Hi, <strong>
<?php echo $username; ?></strong>! You are logged in now. 
<?php echo anchor('/auth/logout/', 'Logout'); ?>
<?php echo $product; ?>

Upvotes: 0

Views: 58

Answers (1)

Kai Qing
Kai Qing

Reputation: 18833

Why don't you just call the products model in index?

function index()
{
    if (!$this->tank_auth->is_logged_in()) {
        redirect('/auth/login/');
    } else {
        $data['user_id']    = $this->tank_auth->get_user_id();
        $data['username']   = $this->tank_auth->get_username();
        $data['product'] = $this->Participant_model->get_all_records();

        $this->load->view('welcome', $data);
    }
}

There's no evident reason to separate that into its own method.

You can also set up class variables and have methods affect them accordingly. That would pretty much depend on your needs though so a proper example may not exactly be applicable.

class Welcome extends CI_Controller
{
    private $data = array(
        'user_id' => null,
        'username' => null,
        'product' => null
    );

    function __construct()
    {
    ...

Then have individual methods affect certain elements in the public $data array

   public function get_participants()
    {
        $this->data['product'] = $this->Participant_model->get_all_records();
    }

and have your index method load the class array instead of populating it in specifically...

function __construct()
{
    parent::__construct();

    $this->load->helper('url');
    $this->load->library('tank_auth_groups','','tank_auth');

    if (!$this->tank_auth->is_logged_in()) {
        redirect('/auth/login/');
    } else {
        $this->data['user_id']    = $this->tank_auth->get_user_id();
        $this->data['username']   = $this->tank_auth->get_username();
    }

    $this->load->model('Participant_model');
}

function index()
{
    $this->get_participants();
    $this->load->view('welcome', $this->data);
}

Mind you, this is just an example of how you can arrange classes to suit your needs. Not necessarily an example of good habits or anything. Ultimately you should program in a logical way that suits your needs and is reasonably readable by any normal human. Just my opinion.

I should say I think it is a bad idea to try to set up a class that forcibly runs every method inside it to create one view. A linear execution is essentially the same thing as piling everything into the index method. Maybe I missed the point of your statement though.

Upvotes: 3

Related Questions