2

I am trying to clean up my site by putting all of my configurations in one place for easy access.

I have many different configuration dependencies for example, PayPal and Stripe public/private and sandbox/live keys as well as a number of links e.g. google recaptcha links.

I don't want to be spreading these keys around my app and then need to go hunting for them if I want to go from sandbox to live for example.

I am trying to define my API keys and most used links in the CodeIgniter config.php file like this...

$config['stripe_live'] = [
    'secret'  => 'secret_key_xyz',
    'private' => 'private_key_xyz',
]

$config['stripe_sandbox'] = [
    'secret'  => 'secret_key_xyz',
    'private' => 'private_key_xyz',
]

$config['paypal'] = [
    'secret'  => 'secret_key_xyz',
    'private' => 'private_key_xyz',
]

$config['recaptcha'] = [
    'site_key'   => 'xyz_one_two_three',
    'secret_key' => 'xyz_one_two_three',
];

$config['jquery'] = [
    ['jquery_link'] => base_url() . 'Public/js/jquery.js',
]

$config['bootstrap'] = [
    ['bootstrap_link'] => base_url() . 'Public/js/jquery.js',
]

$config['fontawesome'] = [

]

$config['google_fonts'] = [

];

$config['groupworld'] = [
    'groupworld_api' => 'api_key_xyz';
];

Question one:

If I wanted to access my Stripe live private key I would have to write...

$stripe_live = $this->config->item('stripe_live');

$stripe_live['public_key'];

This is almost as much work as just copying the key to where I need it (one or two places). So is there a simpler way?

Question two:

Is is okay to put my urls in the config file like in my example above? Or would it be better to define my URLs as constants (in the constants file) and then simply access them as constants instead of writing out $this->config->item('bootstrap_link')

Thanks.

Jethro Hazelhurst
  • 3,230
  • 7
  • 38
  • 80
  • For this you can save these details in your database and than pull fetch these details from data base and set in configuration using this method : $this->config->set_item('key', 'value');. if still want to ask than please comment – Yogesh Shakya Sep 26 '16 at 11:00
  • Interesting solution, but for my purposes I will only be setting these keys once or twice, so going to the trouble of making it dynamic with a database is going a little far for me, correct me if I am missing the bigger picture! – Jethro Hazelhurst Sep 26 '16 at 11:02
  • 1
    You could always make a super global with the value in the index.php of CI's root if it's a static value, or make a custom core file and make sure to extend the core which has your function for this. Though this only allows controllers to call the function. – killstreet Sep 26 '16 at 11:27

2 Answers2

2

After looking at the CodeIgniter Config documentation I have come up with the following solution at least for my API configuration settings, in the example below I am using the google recaptcha API.

1 - Make a new file inside of the application/config folder and call it whatever you want... e.g. api_config.php

Inside this file put your API keys like this:

// stripe api
$config["stripe_live_public_key"] = "public_key_xyz";
$config["stripe_live_private_key"] = "public_key_xyz";

$config["stripe_sandbox_public_key"] = "public_key_xyz";
$config["stripe_sandbox_private_key"] = "public_key_xyz";

// paypal api
$config["paypal_live_public_key"] = "public_key_xyz";
$config["paypal_live_private_key"] = "public_key_xyz";

$config["paypal_sandbox_public_key"] = "public_key_xyz";
$config["paypal_sandbox_private_key"] = "public_key_xyz";

// recaptcha api
$config["recaptcha_api_url"] = 'https://www.google.com/recaptcha/api.js';
$config["recaptcha_verification_url"] = "https://www.google.com/recaptcha/api/siteverify";
$config["recaptcha_public_key"] = "lfgksl;dfg;kkkkdsjfhskjfhkjsdhfjshjksjdh";
$config["recaptcha_private_key"] = "sfkljslfjsjfahjjjjjjhjhsdfjskhajkakkajdj";

// groupworld api

// phpmailer api

2 - In the controller file load your config file and mass the data to the view like this...

    $this->config->load('api_config');
    $data['recaptcha_api_url'] = $this->config->item('recaptcha_api_url');
    $data['recaptcha_public_key'] = $this->config->item('recaptcha_public_key');

3 - In the view file simply display your data...

<script src="<?php echo $recaptcha_api_url; ?>"></script>   
<div class="g-recaptcha" data-sitekey="<?php echo $recaptcha_public_key; ?>"></div>

Now to change your config data in multiple places simply go to the api_config.php file and paste in your new keys.

Jethro Hazelhurst
  • 3,230
  • 7
  • 38
  • 80
1

As I'm a newbie can't comment :/ .

I will start with question 2. Its ok to keep like this. But stripe,paypal are payment gateways it will be good to store it in db as Yogesh said and retrieve to use it.It will also comes in handy if you want to provide user to edit it.

For js,css links you can put them in a view like 'includefiles.php' and load it in all pages as we load views.

for easy retrieval of your data, you can use helper functions.

<?php
//paymentdetail_helper
function getpaymentdetailhelper(someid or gateway name as arg eg.$id){
$ins=& get_instance();
$ins->load->database();

//your queries $ins->db->query();

return $data;
}

?>

Save this in application/helpers as paymentdetail_helper.php and load it as usual. more info about helpers in questionInfo about helper

Its my idea. :) You're welcome with suggestions

Community
  • 1
  • 1
balu anand
  • 81
  • 8