Welcome Guest, Not a member yet? Register   Sign In
Need a little help with a config file for my custom library...???
#1

[eluser]angelleye[/eluser]
I've just recently started working with CI and I love what I see so far. I've got some classes that I've written over the years that I'm converting to CI libraries and I'm struggling to understand how to work with a config file for my library in CI.

The documentation states that...

"You can also pass parameters stored in a config file. Simply create a config file named identically to the class file name and store it in your application/config/ folder. Note that if you dynamically pass parameters as described above, the config file option will not be available."

So I've created that config file to match my library name, but I'm not exactly sure what to do with it after that. I've tried utilizing define() to create globals within this config file and then I also tried something like $myconfig['varname'] but I don't seem to have access to these from within my library when I try and load the app.

Any information on what I'm doing wrong would be greatly appreciated. I'm sure it's something simple, but I'm just not seeing it. Thanks!
#2

[eluser]angelleye[/eluser]
Ah, looks like maybe I need to utilize the config class..?? I'm reading into that now...
#3

[eluser]tomcode[/eluser]
Have a look at the config/config.php file,

format the preferences for You library the same way,

give the file the name of Your library (in lowercase)

the constructor of Your library must take a parameter :
Code:
function __construct($params = array())
{
  foreach($params as $param)
  {
     // Your code here
  }
}

When loading the library the params are passed automatically
#4

[eluser]angelleye[/eluser]
Ok, I seem to have gotten it working, but I'm a little bit confused by what the documentation says.

I've got my config file with $config['options'] inside it. The original class that I'm working with accepts a single parameter (an array of data) similar to how CI passes in an array. So that's what I'm doing, but I'm pulling the values themselves from the config file...

Code:
function PayPal()
    {
        parent::Controller();
        
        // Load helpers
        $this->load->helper('url');
        
        // Load PayPal Adaptive Payments library.
        $this->config->load('PayPal_AP');
        
        $DataArray = array(
        'Sandbox' => $this->config->item('Sandbox'),
        'APIUsername' => $this->config->item('APIUsername'),
        'APIPassword' => $this->config->item('APIPassword'),
        'APISignature' => $this->config->item('APISignature')
        );
        
        $this->load->library('PayPal_AP', $DataArray);    
    }

This seems to be working just fine for me, however, then I noticed the Note in the documentation...

"Note that if you dynamically pass parameters as described above, the config file option will not be available."

Am I doing something completely wrong here or am I ok?
#5

[eluser]tomcode[/eluser]
If You proceed like I did describe, You load the library like this :
Code:
function PayPal()
    {
        parent::Controller();
        
        // Load helpers
        $this->load->helper('url');
        
       $this->load->library('PayPal_AP');    
    }
#6

[eluser]angelleye[/eluser]
Ugh, I'm sorry. For some reason I'm just not following. Here's what I've got.

/system/application/config/PayPal_AP.php contains the following...

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

$config['Sandbox'] = true;
$config['APIUsername'] = 'value';
$config['APIPassword'] = 'value';
$config['APISignature'] = 'value';

/system/application/libraries/PayPal_AP.php contains my library, of course. My original class already received a single parameter (an array of options) but I simply didn't have a default value for it. I went ahead and added that, and then I move on setting options based on whether or not they were passed in via the config array or not...

Code:
function PayPal_AP($DataArray = array())
    {
        
        if(isset($DataArray['Sandbox']))
            $this -> Sandbox = $DataArray['Sandbox'];
.
.
.
}

Then when I try and run with that like you said I'm not getting an error, but the value I'm writing to the page for testing just doesn't write...like it's not finding it correctly.

So I assume that's because I'm doing something incorrectly in my library but that's what I'm missing. You seem to simply be using foreach($params as $param) but then do I use something like $param['option_name'] inside that or what? And then I guess the way my class uses $DataArray already I just need to load $DataArray using values from $param..??

Sorry, I know this is all basics. Just trying to get the hang of CI. Thanks!
#7

[eluser]angelleye[/eluser]
I tried this in my library file and I just get undefined variable, params...

Code:
function PayPal_AP($DataArray = array())
    {
        
        foreach($params as $param)
        {
            $DataArray['Sandbox'] = $param['Sandbox'];
            $DataArray['APIUsername'] = $param['APIUsername'];
            $DataArray['APIPassword'] = $param['APIPassword'];
            $DataArray['APISignature'] = $param['APISignature'];    
        }
        }

So then I tried changing that up a little, since I'm using $config[''] in my config file, I tried this within my class instead...

Code:
$DataArray['Sandbox'] = $config['Sandbox'];
$DataArray['APIUsername'] = $config['APIUsername'];
$DataArray['APIPassword'] = $config['APIPassword'];
$DataArray['APISignature'] = $config['APISignature'];

But then I just get Undefined variables on the $configs.
#8

[eluser]tomcode[/eluser]
You can print out the param, then You know :
Code:
function PayPal_AP($DataArray = array())
{
  print_r($DataArray);
}
#9

[eluser]angelleye[/eluser]
Yeah, I've been doing that. Unless I pass in the options from the controller I'm getting an empty $DataArray. I'm not seeing how I get the values from my config file to work within my library without passing them in from the controller.
#10

[eluser]tomcode[/eluser]
You might have an issue with the Library name, following CI's standards it should be Paypal_ap (all lowercase with the first letter uppercase)

the config filename should then be paypal_ap.php




Theme © iAndrew 2016 - Forum software by © MyBB