1

I have a public function in MY_Controller is_logged, I want to use this in my view file like so:

<?php if( $this->is_logged() ): ?>
    Hey user.
<?php else: ?>
    Please login first.
<?php endif; ?>

but I got an error:

Call to undefined method CI_Loader::is_logged()

How can I use this in my view file?

Regards.

4
  • Possible duplicate of How to call codeigniter controller function from view Commented Mar 24, 2016 at 18:26
  • 1
    You should not call a controller function in a view file. Consider creating a helper and using $CI =& get_instance(); in the helper to get the $CI object if you need it. Commented Mar 24, 2016 at 18:27
  • @Technoh so I can't call controller function in view? So how to check is_logged in view? Commented Mar 24, 2016 at 18:27
  • 1
    CodeGodie's answer is what you are looking for. Commented Mar 24, 2016 at 18:28

1 Answer 1

1

You can only access this property in your controller class not in the view. In your controller's method, access it, then define it in an array so it can then be passed to your view as a variable:

public function test(){
    $this->load->view('test', array('logged'=> $this->is_logged()) )
}

then in your view:

if($logged){
  ...
}
Sign up to request clarification or add additional context in comments.

4 Comments

if you have that error, then you are not passing the variable correctly. That, or the result of $this->is_logged() is NULL. Can you update your original question and include more information on your code?
my bad, now it's beautiful, regards to you.
if you found this answer helpful, please accept it by clicking on the checkmark on the left. Thanks.
I'll in 1 minute, because can't at this moment. (;

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.