Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

codeigniter - using helper in controller doesn't work

I need to use a function in multiple controllers. So I though about using a custom helper, but it seems I can't get it to work. (It works in the view, but I need it in the controller)

It gives me following Fatal Error:

Fatal error: Call to undefined method Developers::checkIfLoggedIn() in /application/controllers/developers.php on line 12

Is it a smart move to use a helper to use a function in multiple controllers, or should I do it otherwise.

Thanks in Advance,
Mark

EDIT:
Controller file:

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

class Developers extends CI_Controller
{
    public function __construct()
    {
         parent::__construct()
         $this->load->helper('form');
         $this->load->helper('url');
         $this->load->helper('login');

         //helper function
         checkIfLoggedIn($this->session->userdata('loggedIn'));
    }
}

Helper file:

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

if (!function_exists('checkIfLoggedIn'))
{
    function checkIfLoggedIn($session_loggedIn)
    {
        $loggedIn = $session_loggedIn;
        if($loggedIn == false)
        {
            redirect('login/');
        }
    }
}

}

like image 661
DijkeMark Avatar asked Aug 15 '12 08:08

DijkeMark


People also ask

Where do I put helper in CodeIgniter?

Helpers are typically stored in your system/helpers, or application/helpers directory. CodeIgniter will look first in your application/helpers directory. If the directory does not exist or the specified helper is not located there CI will instead look in your global system/helpers/ directory.

What is the difference between helper and Library in CodeIgniter?

The main difference between Helper and Library in CodeIgniter is that the Helper is a file with a set of functions in a particular category and is not written in Object Oriented format, while the Library is a class with a set of functions that allows creating an instance of that class and is written Object Oriented ...

What is helper in cakephp?

Helpers are the component-like classes for the presentation layer of your application. They contain presentational logic that is shared between many views, elements, or layouts.

What is custom helper in CodeIgniter?

A CodeIgniter helper is a collection of functions which you could use them within Models, Views, Controllers, and everywhere. Once you load ( $this->load->helper() ) helper file, you can get access to use the functions. Helpers are not written in an Object Oriented format.


2 Answers

In your controller you are using it in wrong way, it's not a method of controller so you can't use $this to call it.

A helper can be loaded anywhere within your controller functions (or even within your View files, although that's not a good practice), as long as you load it before you use it. You can load your helpers in your controller constructor so that they become available automatically in any function, or you can load a helper in a specific function that needs it.

To load a helper you can use

$this->load->helper('name'); // name is the name of the file without extention

Unlike most other systems in CodeIgniter, Helpers are not written in an Object Oriented format. They are simple, procedural functions. Each helper function performs one specific task, with no dependence on other functions.

So, to call a helper function in a controller you should not use

$this->function_name();

instead use

function_name();

For example if you have a helper function in a helper file named myCustomHelper.php as follows

function myHelper()
{
    // code
}

then you can load it in the controller and call it as follows

$this->load->helper('myCustomHelper');
myHelper(); // call the function

but it's better to load helpers in the constructor so it'll be available through the whole script.

Update: If your helper file's name is login_helper.php then you can use it in your controller as follows

$this->load->helper('login_helper');
checkIfLoggedIn($this->session->userdata('loggedIn'));

Read more here.

like image 121
The Alpha Avatar answered Oct 22 '22 03:10

The Alpha


Ok, I know this question has been asked 5 months ago but maybe some people will find this useful. I had just had the same problem and discovered that the filename of my helper functions was a filename that was already used by CodeIgniter.

So if you don't get the warning: 'Unable to load the requested file', but instead get the warning: 'Fatal error: Call to undefined function [function_name], you're probably using a filename that already exists natively.

like image 4
roelleor Avatar answered Oct 22 '22 04:10

roelleor