If I have everything I need on all pages, I do the following.
- Add helper .
- Add a new helper to autoload .
- Just call your function where you need it.
The reason I do this is because if you need it on many different controllers, then it is very convenient in one easily accessible place. When you need to maintain your code, this will make it a lot easier. For example, if you want to add caching, you can simply add this code to this single function instead of changing it in each controller.
Infact, in your particular case, I actually created my own session class to extend the CI session. As a basic example (I also use sessions in the database ):
class MY_Session extends CI_Session { public function __construct() { parent::__construct(); } public function login($email, $password) { $CI =& get_instance(); $CI->load->model('usermodel', 'user'); $result = $CI->user->login($email, $password); if($result === false) { return false; } else { $this->set_userdata('email', $result->email); return true; } } public function is_logged_in() { $email = $this->userdata('email'); if(!empty($email)) { return true; } else { return false; } } }
Thus, since you are likely to store this information in sessions, it makes sense to store this information together.
source share