How to Create a Config Variables Loader in CodeIgniter: New Premium Tutorial
plusvideos

How to Create a Config Variables Loader in CodeIgniter: New Premium Tutorial

Download Source Files

If you’ve been using CodeIgniter or another MVC framework for any length of time, you’ve probably started to realize the advantage of abstracting data and logic from presentation. But efficiency is not without its drawbacks, and, in the case of CodeIgniter, one of these drawbacks is the need to constantly bounce variables out of your controllers so that they are available within your views.

In this tutorial, I’ll show you a really cool way to eliminate this hassle. Become a Premium member to gain access to this tutorial, as well as hundreds of other tutorial, screencasts, and freebies from the Tuts+ network.

We’re going to build a config loader that automatically loads all of your important site variables and makes them available globally. Even more cool, your site variables will be stored in a separate config file that you can append or edit whenever you like. Not only will this save you a lot of time (and errors!), but it will help you develop a killer workflow that you can build on for all your projects. Lets get started!


Join Net Premium

Screencasts and Bonus Tutorials

For those unfamiliar, the family of Tuts+ sites runs a premium membership service. For $19 per month, you gain access to exclusive premium tutorials, screencasts, and freebies from Nettuts+, Psdtuts+, Aetuts+, Audiotuts+, Vectortuts+, and CgTuts+. For the price of a pizza, you’ll learn from some of the best minds in the business. Become a Premium member to read this tutorial, as well as hundreds of other advanced tutorials and screencasts.

Note: Want to add some source code? Type <pre><code> before it and </code></pre> after it. Find out more
  • Carlos

    Another Codeigniter Tutorial!!!

    I’m so happy with my premium account!

    Thakyou!

  • http://www.circuitbomb.com Dustin

    Thank you for this tutorial, one more reason premium accounts are worthwhile.

  • Brad

    Excellent. Thank you Daniel for a well done tut. And thanks to Nettuts for another Codeigniter tut!

  • Daniel

    Its a shame the url to the video doesnt work atm =(

    been looking forward to these great CI tuts

    • http://www.jeffrey-way.com Jeffrey Way

      That’s odd. It’s working for me.

  • Gasaki

    Is it possible to make the global vars available in the controllers too?

    • Daniel Balfour
      Author

      You can certainly access the variables in your custom config file within your controllers but you’d first have to load the config file. You can do that by either adding ‘globals’ to your $autoload['config'] = array(); in the autoload file, or by loading it from your controller using $this->config->load(‘globals’);.

      Once loaded, you access the individual variables using $foo = $this->config->item(‘foo’), where ‘foo’ is the index of the $config array.

  • Myke

    Can you say what format this tutorial is in – video, or print?

    • Robert

      it is in video format

    • http://www.jeffrey-way.com Jeffrey Way

      Video

  • http://www.goodshirtdesign.com Pete

    Great tutorial and very valuable to know when using CodeIgniter! I’ve just started using CI this past year and I’ve found the Nettuts video series extremely helpful in learning it.

  • Sid

    I’m curious about this tutorial.

    Is it any different from creating your own config file and autoloading it? That makes it available globally.

    • Daniel Balfour
      Author

      You bet.

      Autoloading a custom config file allows you to individually poll each config['index'] using something like $this->config->item(‘index-name’). While this does work, It can be a pain to have to do that for each individual config item you need, then to pass that variable to a $data[] array, and then to either pass the $data[] array to a view or to load it using $this->load->vars($data).

      Using the config-loader, you basically ‘set it and forget it’. You’ll create a config file and the corresponding config-loader class just once. Then, every time you want to access any of your config-variables (globally) from any view, they are just magically there.

      • Lex Beelen

        And if you want to load your global vars in a controller? You have to use this method $this->config->item(‘item’); or not?

      • Daniel Balfour
        Author

        Lex,

        Yep, that’s about right, though you’d have to have your config file loaded (or autoloaded) first. See above in my response to Gasaki.

  • Danni

    Keep them CI tuts coming ;) !

  • Daniel Balfour
    Author

    Hey Gang!

    Really glad you liked this. Thanks for all your terrific comments & feedback!

    I might do a follow-up on this one showing how to automatically ‘prefix’ your global index names, so that you can basically use the globals class as a template which you can then apply to any CI project without changing a thing.

  • Bobby

    Nice tutorial. What are the advantages of using this technique over defining constants in config/constants.php? Thanks

  • cahva

    Whats the point of foreching the same array that you could just pass to the $CI->load->vars()? You are basically creating the same array with different name(thus wasting a little CPU & memory).

    foreach ($config as $key=>$value)
    {
    $data[$key] = $value;
    }

    $CI =& get_instance();
    $CI->load->vars($data);

    VS:

    if (is_array($config)) // just to make sure its an array
    {
    $CI =& get_instance();
    $CI->load->vars($data);
    }

    • cahva

      Where’s my edit button :) The last should offcourse be:

      $CI->load->vars($config);

      BTW, I used (for the first time) the pre tags when adding comment for the code but didnt work for some reason..

    • Robert

      yeah, I was wondering the same thing. I couldn’t think of any reason why you would have to loop through the $config array.

      • Adam

        I thought the same thing, but I guess with the loop, if you wanted to do any other processing or validation to the config items before handing them off, you could do it there.

  • http://www.airwolfe.com alex

    Not sure why no one ever mentions this but the “Login” button should be more available throughout the site.

  • Cesar

    Knowledge should be free. Aren’t you satisfied with all the money you make with ads?

    net.tuts-

    • http://www.jeffrey-way.com Jeffrey Way

      Doesn’t pay the bills. For that matter, every book in the bookstore should be free as well. Oh, and your school teachers – they should work for free as well.

  • Henry

    Wow once again I can’t download the files or watch the video.

    Please fix your Subscription code !!!!!

    I’m so tired of this ….

  • QUERO ASSITIR

    Karalho q porra
    Quero assitir karalhooooooooooooooo