Welcome Guest, Not a member yet? Register   Sign In
CI2 - Is there an issue with loading libs?
#11

[eluser]techgnome[/eluser]
Well, if all you needed was the config info.... try something like this. This is an actual code piece from a library I've built... and it works just fine getting custom config items.
Code:
class Gnome_news
{
    /**
     * CodeIgniter global
     *
     * @var string
     **/
    [b]protected $ci;[/b]

    // $gn_config - stores the configuration settings as loaded from the newsfeed config file
    var $gn_config = array();
    /**
     * __construct
     *
     * @return void
     *
     **/
    public function __construct()
    {
        [b]$this->ci =& get_instance();
        $this->ci->load->config('gnome_news', TRUE);
        $this->gn_config = $this->ci->config->item('gnome_news');[/b]
        $this->ci->load->helper('date');
        $this->ci->load->helper('url');
        $this->ci->load->model('gnome_news/gnome_news_model');
    }

I've bolded the important bits... In short, I get the CI super var instance, and use it in the constructor to load the config and other items. When loading the config, pass the TRUE parameter to load it into it;'s own array, then use that to load it into an array inside the library.

-tg
#12

[eluser]stormbytes[/eluser]
Hey TechGnome -

Well thanks for sharing! I'll certainly give it a go.

Would you mind explaining what the following statement does? I've seen it all over the place. I'm happy to read up on it if you could point me in that direction as well -

Much of the reason I've been trying to 'extend' the base controller class is so as to gain access to its methods. Seems this way you accomplish the same thing.

I'd love to wrap my head around it. I'm sure it'd come in handy.

Thanks!

Code:
=& get_instance()
#13

[eluser]techgnome[/eluser]
Does exactly as I mentioned - gets t CI sure class instance.
More info on it in the UG: http://ellislab.com/codeigniter/user-gui...aries.html
Specifically half way down, titled "Utilizing CodeIgniter Resources within Your Library"

-tg
#14

[eluser]stormbytes[/eluser]
Read up on that -

THANK YOU Smile
#15

[eluser]SitesByJoe[/eluser]
Why haven't you just made a custom config file and drop it in you application's config directory? If all you're doing is setting application variables it's the way you should be doing it in my opinion.

I make one for every project - just simple variables really:

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

/*
|--------------------------------------------------------------------------
| Website Settings
|--------------------------------------------------------------------------
|
| To help speed up the initial content insertion, I'm setting some site-wide
| defaults so I don't have the wrong client's name showing with one quick
| change.
|
| Next version of this CMS needs to put this stuff in a data table so it can
| set up during an "install" process?
|
*/

// Company Information
$config['company_name']    = "XXXX";
$config['company_address'] = "XXXX";
$config['company_city'] = "XXXX";
$config['company_state'] = "XXXX";
$config['company_zip'] = "XXXX";
$config['company_phone'] = "XXXX";
// etc...

// Company Contact Person
$config['contact_person'] = "Joe Taylor";
$config['contact_email'] = "[email protected]";

// Mail Server Info
$config['mail_server'] = "XXXX";
$config['mail_username'] = "XXXX";
$config['mail_password'] = "XXXX";

// Mailbuild Info
$config['mailbuild_url'] = "XXXX";
$config['mailbuild_name'] = "XXXX";
$config['mailbuild_email'] = "XXXX";

// Google Info
$config['google_map_key'] = "XXXX";
$config['google_analytics_key'] = "XXXX";
$config['google_libraries_api_key'] = "XXXX";

// Akismet Info
$config['akismet_key'] = "XXXX";

// Twitter Info
$config['twitter_username'] = "XXXX";
$config['twitter_password'] = "XXXX";

// Default Meta Data for pages
$config['site_name'] = "XXXX";
$config['meta_title'] = "XXXX";
$config['meta_description'] = "XXXX";
$config['meta_keywords'] = "XXXX";

// Hosting Environment Items
$config['site_directory'] = "XXXX";


/* End of file application.php */
/* Location: ./application/config/application.php */

The you load it with the autoload.php in your config file:

Code:
$autoload['config'] = array('application');

Every variable you set in the file can be retrieved by:

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

Makes my life alot easier. If you were smarter than me you might use a database table instead and bundle it's population with an install procedure.
#16

[eluser]stormbytes[/eluser]
Hey Peeps!

Thanks for all the terrific suggestions!

I ultimately went with a hybrid solution. Rather then relying on constantly having to call custom config values, I setup a config file and then a library class (same name) that accepts the config file as a param (MyClass($config_file)) and within the constructor, the params are loaded as superglobals.

The result -

I can access any of my site-wide config vars no problem, from anywhere, anytime. No loading. No having to remember anything. Plus, to make the code more portable, I added a '_prefix' config param so that I can prefix my global vars.

If anyone's interested in the code/class I'm happy to post it. Hollar at me.

Cheers!




Theme © iAndrew 2016 - Forum software by © MyBB