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

#1
[eluser]core-coder[/eluser]
HI,

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

Thanks

#2
[eluser]bretticus[/eluser]
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.)

#3
[eluser]core-coder[/eluser]
Thanks, how can I access database from a helper function ?

#4
[eluser]WebMada[/eluser]
As bretticus said, helpers contain just useful functions
For me, I put date and string treament functions in it

#5
[eluser]bretticus[/eluser]
[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:
Code:
// A simple model

class Forms extends Model {
    
    function Forms() {
        parent::Model();
    }
    
    function get_states()
    {
        $states = array(
            'AZ'=>'Arizona',
            'AK'=>'Alaska',
            'AL'=>'Alabama',
        );
        
        // it's okay to have logic in here that
        // renders the list or even generates portable
        // output.
        
        return $states;
    }
}

Code:
//Simple controller to illustrate

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

Using the $states data in the view...

Code:
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
    "http://www.w3.org/TR/html4/loose.dtd">
&lt;html lang="en"&gt;
    &lt;head&gt;
        &lt;title&gt;My View&lt;/title&gt;
    &lt;/head&gt;
    &lt;body&gt;
        &lt;?=form_open('email/send')?&gt;
        &lt;?=form_dropdown('states', $states)?&gt;
        &lt;input type="submit" name="submit" value="go"&gt;
        &lt;?=form_close()?&gt;
    &lt;/body&gt;    
&lt;/html&gt;

#6
[eluser]core-coder[/eluser]
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

#7
[eluser]bretticus[/eluser]
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!

#8
[eluser]core-coder[/eluser]
Thank you Bretticus, much appreciated


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


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