3

I'm trying to build a custom library which will contain functions that would be available through the whole site. Inside /application/libraries I've made a new file CustomFuncts:

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/**
 * @brief CommonFuncts      
 * This class is used to provide common user functions to the whole application.
 *
 * @version 1.0
 * @date May 2012
 *
 * 
 */
class CommonFuncts extends CI_Controller {

 protected $ci;

/**
 * function constructor
 */
function __construct()
{
    $this->ci =& get_instance();
}
 /**
* @brief checkCookie
* 
* Checks for a previous cookie, and if exists:
* @li Loads user details to CI Session object.
* @li Redirects to the corresponding page.
* 
*/
public function verificaCookie(){
    $this->ci->load->view('index');
}
 }
/* End of file CommonFuncts.php */
/* Location: ./application/libraries/CommonFuncts.php */

And in controller welcome.php:

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

class Welcome extends CI_Controller {

/**
 * Index Page for this controller.
 *
 * Maps to the following URL
 *      http://example.com/index.php/welcome
 *  - or -  
 *      http://example.com/index.php/welcome/index
 *  - or -
 * Since this controller is set as the default controller in 
 * config/routes.php, it's displayed at http://example.com/
 *
 * So any other public methods not prefixed with an underscore will
 * map to /index.php/welcome/<method_name>
 * @see http://codeigniter.com/user_guide/general/urls.html
 */
public function index()
{
    $this->load->library('commonFuncts');
    $this->commonFuncts->verificaCookie();  
}
}

/* End of file welcome.php */
/* Location: ./application/controllers/welcome.php */

I'm getting the following error message:

A PHP Error was encountered

Severity: Notice

Message: Undefined property: Welcome::$commonFuncts

Filename: controllers/welcome.php

Line Number: 23

Fatal error: Call to a member function verificaCookie() on a non-object in
/var/www/vhosts/unikodf.com/httpdocs/application/controllers/welcome.php on line 23

I've tried many things, including extending in the library the CI_Controllerand using $this->load->view' instead of$this->ci->load->view` but still I'm getting the same message.

Cœur
  • 37,241
  • 25
  • 195
  • 267
Luis M. Valenzuela
  • 149
  • 1
  • 2
  • 10
  • I think using camel case in a library name is not supported in codeigniter.So CmmonFunct should be renamed to Common_funct.Isn,t it – jayadevkv Feb 04 '14 at 03:57

5 Answers5

10

If your library extends CI_Controller. That means that you are actually extending the functionality of the controllers. Instead declare your library like this:

class CommonFuncts { }

You don't need to inherit from CI_Controller, since you are writing a library not a controller and a library is not part of the MVC model. It's single class which extends the features of the framework, fixes a common issue or has a functionality used by many controllers.

To access it's methods use:

$this->load->library('CommonFuncts');
$this->commonfuncts->verificaCookie();

If you want to simplify the name with which you call the library use:

// The second argument is the optional configuration passed to the library
// The third argument is the name with which you would like to access it
$this->load->library('CommonFuncts', '', 'common');
$this->common->verificaCookie(); //  /\ = configuration
//     /\/\/\ = name
Itay Grudev
  • 7,055
  • 4
  • 54
  • 86
  • 1
    If you want to use the SIMPLER way then you must pass 3 parameters: `$this->load->library('CommonFuncts', null, 'common');` – Mike Jul 31 '13 at 02:55
  • You are right, though according to the doc it should be an empty string (`''`) rather than `null`. – Itay Grudev Aug 01 '13 at 08:54
4

You're confusing controllers with libraries.

Custom libraries should not extend CI_Controller. The documentation outlines how to create your own custom library.

Secondly, you need to load the library before you can access its methods:

$this->load->library('CommonFuncts');
// The "CommonFuncts" library is now ready to use
$this->commonFuncts->verificaCookie(); 
Colin Brock
  • 21,267
  • 9
  • 46
  • 61
  • Solved !!! It seems that I was making a typo in line 23 instead of using `$this->commonfuncts->verificaCookie()`I was using `$this->common**F**unts->verificaCookie()`. Thanks anyway ! – Luis M. Valenzuela May 18 '12 at 17:02
  • @LuisM.Valenzuela I'm glad you got it working. I highly recommend reading the documentation pages I linked to anyway. Hope you find them helpful. – Colin Brock May 18 '12 at 17:03
  • Thanks Colin, I've read them, but as you might guess after a couple of days struggling to get it right I tried almost every method I read over the internet, and one of them is extending CI_Controller... – Luis M. Valenzuela May 18 '12 at 17:07
0

Here I want to modify one thing (I am using code igniter 2.1.3):

//echo CI_VERSION;  to get the code igniter version
Plesae use
$this->commonfuncts->verificaCookie(); //f is small here commonfuncts
instead of
$this->commonFuncts->verificaCookie();
Jay Bharat
  • 33
  • 3
0

Your constructor is not correct. You used:

$this->ci =& get_instance();  //This is invalid syntax. 

Assign codeIgniter to a variable and use that variable on codeigniter's resources

class exampleClass{
    protected $codeIgniter;

    public function __construct()
    {
        $this->codeIgniter =& get_instance();
        $this->codeIgniter->load->helper('url');
        $this->codeIgniter->config->item('base_url');

    }
}
FlipperPA
  • 13,607
  • 4
  • 39
  • 71
makinyelure
  • 165
  • 2
  • 7