csotelo
csotelo

Reputation: 1485

Create Codeigniter Helper -> undefined method

I created a helper "session_helper.php" in application/helpers/ folder

<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');

if ( ! function_exists('is_login'))
{
    function is_login()
    {
        $CI =& get_instance();

        $is_logged_in = $CI->session->userdata('is_logged_in');

        if (!isset($is_logged_in) || $is_logged_in != TRUE) { 

            redirect('login');

        }               

    }   
}

And "Configuracion" Controller:

class Configuracion extends CI_Controller {

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

            $this->is_logged_in();  
    }

    function is_logged_in()
    {
        $this->load->helper('session');

        $this->is_login();

    }       

} 

The problem is when I call the controller "http://localhost/proyect/configuracion" I get the following error:

Fatal error: Call to undefined method Configuracion::is_login() in C:...\application\controllers\configuracion.php on line 15

I read the manual and apparently everything is correct ... what is wrong?

Upvotes: 1

Views: 4725

Answers (2)

Jakub
Jakub

Reputation: 20475

Helpers are not methods, they are just simple function calls.

Take a look at helpers in the User Guide: http://codeigniter.com/user_guide/general/helpers.html

Loading a helper:

$this->load->helper('url');

using its helper functions:

<?php echo anchor('blog/comments', 'Click Here');?>

where anchor() is the function that is part of the loaded helper.

Also I would urge you to stay way from calling a helper 'session' make it something more descriptive, as it might become confusing later on. Just a suggestion, its entirely up to you.

Upvotes: 1

JanLikar
JanLikar

Reputation: 1306

"is_login" is a function, not a method. Just replace $this->is_login(); with is_login();.

Upvotes: 4

Related Questions