CodeIgniter Forums
Listing all models, libraries, helpers once ? - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23)
+--- Thread: Listing all models, libraries, helpers once ? (/showthread.php?tid=51559)

Pages: 1 2


Listing all models, libraries, helpers once ? - El Forum - 05-09-2012

[eluser]sayo[/eluser]
Hi,

Is it possible to have everything I will need for
my codes listed just once, so I don't have to re-write
this kind of code on each page:

Code:
class A_complete extends CI_Controller {

public function __construct()
{
  parent::__construct();
  
  $this->load->library('session');
  $this->load->database();
  
  $this->load->helper(array('url', 'security', 'date', 'redirect'));
  $this->load->model('reuse_model');
  $this->load->model('complete_model');
  $this->load->model('a_login_model');
    
}
}


If I eventually have 100 pages and need to change

Code:
$this->load->helper(array('url', 'security', 'date', 'redirect'));

to

Code:
$this->load->helper(array('url', 'security', 'date', 'do_redirect'));

It means I have to change it in 100 places. Can this
be avoided?




Listing all models, libraries, helpers once ? - El Forum - 05-09-2012

[eluser]Fons Vandamme[/eluser]
put this piece of code in MY_Controller?


Listing all models, libraries, helpers once ? - El Forum - 05-09-2012

[eluser]sayo[/eluser]
Thanks.

Trying this out. I believe you are
saying I should extend codeigniter's
core. Then add what I need in there.

Right?


Listing all models, libraries, helpers once ? - El Forum - 05-09-2012

[eluser]Fons Vandamme[/eluser]
Yep,

This link has all the info you need: http://codeigniter.com/wiki/MY_Controller_-_how_to_extend_the_CI_Controller.

You let your controllers extends MY_controller and MY_controller extends CI_controller in that setup.


Listing all models, libraries, helpers once ? - El Forum - 05-09-2012

[eluser]sayo[/eluser]
Thanks!

Got it.


Listing all models, libraries, helpers once ? - El Forum - 05-09-2012

[eluser]CroNiX[/eluser]
You can also autoload anything you need without extending anything (one less file to load/process which is more efficient).


Listing all models, libraries, helpers once ? - El Forum - 05-09-2012

[eluser]Stefan Hueg[/eluser]
[quote author="CroNiX" date="1336582686"]You can also autoload anything you need without extending anything (one less file to load/process which is more efficient).[/quote]

...but it should also not be overused because file access is always slow Wink


Listing all models, libraries, helpers once ? - El Forum - 05-09-2012

[eluser]CroNiX[/eluser]
Right, which is why doing it in autoload (which always gets processed) is more efficient than extending the controller (which requires an additional file to be loaded). There are other good reasons for extending the controller, but if it's only to load assets that can be autoloaded, it's kind of a waste.


Listing all models, libraries, helpers once ? - El Forum - 05-09-2012

[eluser]Stefan Hueg[/eluser]
In my projects, I always use and need a MY_Controller and load the classes/helpers in three stages:

Stage 1 (autoload): Things which I really need on each request (database, session etc.)
Stage 2 (constructor): Things which I need all over the whole controller in each function
Stage 3 (in-function): Things which I need only locally in my functions

And it works well. It should not happen often that you have to rename a controller/model/whatever.

But in that case, several PHP IDEs offer a search/replace function which will do the job pretty well.


Listing all models, libraries, helpers once ? - El Forum - 05-09-2012

[eluser]CroNiX[/eluser]
All fine and good, but it doesn't really have to do with the question in the original post. He only wanted to load those assets in one place so he didn't have to do it at the top of all controllers. Autoload is the proper place for that assuming (from his post) that's all he wanted to do. As I said, extending the controller has advantages, but his question didn't indicate he was looking for anything other than loading basic assets. If he wanted to do more that that, then I would agree that MY_Controller is the way to go. Otherwise, it's plain less efficient to only use MY_Controller to "autoload" assets when CI has a built in way of doing that (which happens on every request anyway) as it requires an additional file to be loaded, which as you pointed out "file access is always slow". So, I'm not really sure what your point is bringing that up with only loading assets.