0

I'm rather new to Codeigniter, so I may be going about this the wrong way. In the header for my template, I have a spot for displaying account information or a message to log in.

In order for it to work properly, each controller obviously need to have the same code. To avoid this, the user guide says I should be able to extend CI_Controller and it automatically includes that code. However, that's not working for me. Here's what I've got.

application/core/MY_Controller.php:

<?php
class MY_Controller extends CI_Controller {
function __construct()
{
    parent::__construct();
    $this->load->database();
    $this->load->model('user_model');

    if ( $this->user_model->validateToken ( $this->input->cookie('session_token', TRUE) ) ) 
    {
        $data['login_info'] = 'Logged in as '. $this->user_model->getUsernameAsLink($this->input->cookie('session_token', TRUE)).'<BR />
        <a href="/dashboard">Control Panel</a>';
    }
    else
    {
        $data['login_info'] = 'You are not logged in<BR />
        <a href="/account/login">Log In</a>';
    }      
}
}
?>

the model it references:

<?php
class User_model extends CI_Model {

public function __construct()
{

}

public function validateToken($token)
{
    // Get token from database to check against cookie
    $query = $this->db->query('SELECT `login_token` FROM `Users` WHERE `login_token` = "'.$token.'"');

    // Match Found?
    $rowCount = $query->num_rows();

    if ( $rowCount == 1 ) {
        return true;
    } else {
        return false;
    }
}

public function getUsernameAsLink ( $token )
{
    // Get token from database to check against cookie
    $query = $this->db->query('SELECT `username` FROM `Users` WHERE `login_token` = "'.$token.'"');

    foreach( $query->result() as $row ) {
        $username = $row->username;
    }

    $returnString = '<a href="/profile/'. $username .'">'.ucfirst ( $username ).'</a>';

    return $returnString;    
}
}
?>

I'm getting notice errors saying that the $data['login_info'] value doesn't exist. Is there anything I've left out to keep it from processing the extension to MY_Controller?

6
  • Did you make all your other controllers extend MY_Controller instead of CI_Controller? Commented Oct 11, 2012 at 2:25
  • Yes, all my controllers are currently extending MY_Controller Commented Oct 11, 2012 at 2:32
  • Where does it say that it doesn't exist? In the code you included, there isn't anywhere it is being used, only set. Commented Oct 11, 2012 at 2:43
  • @CWSpear In Codeigniter, the variables are called by their key in $data in the view. I omitted this because it's simply <?php echo $login_info ?> Commented Oct 11, 2012 at 3:06
  • Do a print_r($data) right before you call the view. Or better yet, if you do it like in my answer, print_r($this->data). Commented Oct 11, 2012 at 3:16

1 Answer 1

1

For $data to be available in your other controllers, you need to make it available. Try setting it to $this->data and using that same thing in the other controllers.

Sign up to request clarification or add additional context in comments.

3 Comments

I changed all $data references in my controllers to $this->data and passed it to my views, but it still doesn't pass the data from MY_Controller to the next controller. Is there something else that needs to be done?
Are you using it as $this->data in both MY_Controller and your other controllers?
Using $this->data in all controllers from here on out. Thanks!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.