• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
what are the differences between libraries, helpers and plugins in CI ?


Any one knows what are the differences between libraries, helpers and plugins in CI?
Any useful links ?


Besides the manual that explains these all quite well...

libraries: Utility classes where object state is important (payment gateways, authentication, etc.)

helpers: Collections of related functions (not classes) that do repetitive tasks (strings, arrays, etc.)

plugins: A simple way to drop in third party classes. Typically, the whole process is called with a single wrapper function. (deprecated in the upcoming version 2.0 of CodeIgniter.)

Thanks, how can I access database from a helper function ?

As bretticus said, helpers contain just useful functions
For me, I put date and string treament functions in it

[quote author="core-coder" date="1268735927"]Thanks, how can I access database from a helper function ?[/quote]

Unless you need a quick function from a view, I recommend you explore a third option. That option being a model.

Models are typically associated with database functionality, but when you "look under the hood" you'll find they are essentially the same as a controller without routing capability. In other words, anything you can do in a controller, you can do in a model. This makes models a good repository for storing repetitive code in a portable (or modular fashion.) Model methods do not have to return database records, however, it good practice to return some sort of data. Whether it be a computed string or an array, it's best to hand that off to a view to decide how the data should be presented.

Okay, there's my two cents on models. Now for an example:
// A simple model

class Forms extends Model {
    function Forms() {
    function get_states()
        $states = array(
        // it's okay to have logic in here that
        // renders the list or even generates portable
        // output.
        return $states;

//Simple controller to illustrate

class Pages extends Controller {
    function Pages() {
    function index() {
        $data['states'] = $this->forms->get_states();
        $this->load->view('form', $data);

Using the $states data in the view...

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
&lt;html lang="en"&gt;
        &lt;title&gt;My View&lt;/title&gt;
        &lt;?=form_dropdown('states', $states)?&gt;
        &lt;input type="submit" name="submit" value="go"&gt;

Thank you dude, it looks great.

For me I want to upload(and update) an image from many functions in my controller. For that shall I create a model for my uploading options. so I just want to include the model then I can call the file upload (or update)function whenever I need, Shall I move forward with this type of model ?

Thanks again

I see no reason why this couldn't be implemented in a model. Particularly because it involves updating database records, etc. Remember, the purpose would be for porting this functionality across various areas of your application. Thus, try to make it as modular as possible (provide a way to change settings, etc from outside the model and pass them in for a particular use.)

Good luck!

Thank you Bretticus, much appreciated

Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  

  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2021 MyBB Group.