Welcome Guest, Not a member yet? Register   Sign In
How to call a config item
#1

[eluser]satie[/eluser]
Why if I use

Code:
$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:

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

Thanks
#2

[eluser]PhilTem[/eluser]
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
#3

[eluser]InsiteFX[/eluser]
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.
Code:
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');
#4

[eluser]satie[/eluser]
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
Code:
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:
Code:
function pre_password($password)
    {
         return sha1($password.$this->config->item('encryption_key'));
    }
#5

[eluser]InsiteFX[/eluser]
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.
#6

[eluser]satie[/eluser]
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.





Theme © iAndrew 2016 - Forum software by © MyBB