CodeIgniter Forums
[EASY] Best practice for this (MVC) - 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: [EASY] Best practice for this (MVC) (/showthread.php?tid=56815)



[EASY] Best practice for this (MVC) - El Forum - 01-22-2013

[eluser]NeoGeneration[/eluser]
Hello there,

I have some functions that connect to a model and to a library. These functions were initially in a controller but now I'm using the same logic in another controller.

I was wondering whats the best practice here to get the code tidy.

I tried to convert my functions to a library or helper but I got lost and I don't know if that is what I should do.

Thanks for that.


[EASY] Best practice for this (MVC) - El Forum - 01-22-2013

[eluser]boltsabre[/eluser]
Libraries are more for if you need to create a class/object, whereas helpers are just for "function" storage.

So depending on what you're doing, either should be fine for this kind of situation.


[EASY] Best practice for this (MVC) - El Forum - 01-22-2013

[eluser]NeoGeneration[/eluser]
So I should create a helper? This can access libraries and models?


[EASY] Best practice for this (MVC) - El Forum - 01-22-2013

[eluser]boltsabre[/eluser]
Quote:So I should create a helper?
No idea, as I said, it really depends on what you're trying to do. If it's just a function then fine, but if you need to create a class/object then you should do it in a library.
Yes, you can load libraries/models/etc in helpers (and in your own libraries).
But you cannot use $this, it has to be $CI... just google "codeigniter how to load a library in a helper" for more details.


[EASY] Best practice for this (MVC) - El Forum - 01-22-2013

[eluser]NeoGeneration[/eluser]
Thanks a lot! will try that. Smile