• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to call a config item

Why if I use

$var  = $this->config->item('encryption_key');

CI returns
PHP Fatal error: Call to a member function item() on a non-object ...

The documentation says:
To retrieve an item from your config file, use the following function:

$this->config->item('item name');


Give us a little more code that runs prior and after your $var = ... code. If you're not in an object environment or have not assigned the CI super object to $this (i.e. you have that line in a library and not a controller or model) then you cannot use $this

It depends on where you are calling it from, if you are trying to call it from a library or helper then you need to use the CodeIgniter super object.
class Some_lib {

  * The CI super object
private $CI;

public function __construct()
  $this->CI = get_instance();

// then to access a config item you would do it like this
$var  = $this->CI->config->item('encryption_key');

Yes, sorry about lack of code.

@InsiteFX, thanks for your answer but if i put your code in my model CI returns
Quote:Severity: Warning

Message: Missing argument 1 for M_npanel::__construct(), called in D:\inetpub\vhosts\website.it\httpdocs\np_system\core\Loader.php on line 303 and defined

Filename: models/m_npanel.php

Line Number: 5

My question with more details
I have a model function
public function insert()
        $dati_annuncio = array(
            'password' => $this->funzioni->pre_password($this->input->post('password'))

So in my custom "funzioni" there is the 'pre_password' function:
function pre_password($password)
         return sha1($password.$this->config->item('encryption_key'));

Sorry about that I forgot to remove the $config from the constructor It came from one of my libraries, I fixed the code above it should work now.

Ohh, I'm so stupid: I had not noticed.
Works! Thanks

I put your code not in the model, but in my custom class "funzioni" (./application/libraries/funzioni.php);

Thanks to @PhilTem too.

Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  

  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2021 MyBB Group.