• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Is it Allowed to Make loading of Resources Global to a controller?

#1
[eluser]n00bPhpCoder[/eluser]
instead of loading them via functions can we do this?


say

function index(){
$this->load->model('downloadmodel','dl');
$data['downloads']=$this->dl->getEntries(2);
$this->load->view('home',$data);
}

instead of loading the model in index function i need to load it globally so that other functions can use the model too.

like can i load it on constructor

function Home()
{
parent::Controller();
$this->load->model('downloadmodel','dl');
}

so that the model object is also available on other functions?

function no1{
$this->dl->something
}

function no2{
$this->dl->something

}

Any Ideas?

#2
[eluser]Derek Allard[/eluser]
Currently models cannot be autoloaded, but it does seem like that would be useful.

#3
[eluser]n00bPhpCoder[/eluser]
Right Sad i was still thinking of Windows Programming Smile.

Im a bit new to CI so if ever my ways is not right pls correct me. BTW, does CI auto unload objects i created on the process? or i have to manually unload models i created?

#4
[eluser]Derek Allard[/eluser]
No need to unload anything - resource allocation is handled pretty well.

Oh yeah, and welcome to CodeIgniter!

#5
[eluser]n00bPhpCoder[/eluser]
Wow I am really Impress with it Smile you can see here what ive done using code igniter. Still exploring tons of possiblities using CI Smile

http://121.97.156.37

#6
[eluser]marcoss[/eluser]
[quote author="n00bPhpCoder" date="1184569242"]instead of loading them via functions can we do this?

Any Ideas?[/quote]

If you mean loading the Model in the controller constructor, that's perfectly fine, i use it like this.

Code:
<?php
    
function Events(){
    parent::Controller();
    $this->load->model('EventsModel');    
}

function index(){
    $data = array(
        'title'        => '» Events',
        'events'    => $this->EventsModel->getEvents(),
    );
    $this->load->view('eventsList', $data);
}

function event($id=0){
    $data = array(
        'title'        => '» Event',
        'event'    => $this->EventsModel->getEventDetail($id),
    );
    $this->load->view('eventDetail', $data);
}

?>


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


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