CodeIgniter Forums
Reoccuring code - 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: Reoccuring code (/showthread.php?tid=1866)



Reoccuring code - El Forum - 07-02-2007

[eluser]zdknudsen[/eluser]
I have two code snippets that should be placed under multiple controllers.

First I have the user-validation/user-data-retrieval code that should be put in pretty much every controller (except ajax and a few other controllers), and then I have some code for fetching data about a particular game which will should be included in about half of my pages.

How would I do this? Libraries? Helpers? Plugins? Models even?

Cheers,
Zawk


Reoccuring code - El Forum - 07-02-2007

[eluser]Bulk[/eluser]
In my head, it breaks down like this:

Anything to do with database interaction, or retrieval of data should be placed in a model.

If you have a large set of functions that are related and will be used in multiple places that DON'T interact with a database (a user authentication library, XML library, validation etc) then it should be a library.

If its just a single or a few functions that don't really interact with other functions in any way (for example a function to format a string in a certain way), then it should be in a helper.

HOWEVER each programmer will find their own methodologies for using libraries, models and helpers - thats one of the great things about CI - it doesn't force you to do it one way or another. You should do it the way that makes most sense to you.


Reoccuring code - El Forum - 07-02-2007

[eluser]zdknudsen[/eluser]
I think I'm gonna go with a library for user-validation and a model for game-data retrieval (: Thanks.


Reoccuring code - El Forum - 07-02-2007

[eluser]Phil Sturgeon[/eluser]
Agreed, that's how I would do it. Game data? Making a game or a game site?


Reoccuring code - El Forum - 07-02-2007

[eluser]zdknudsen[/eluser]
I'm doing a complete remake of a game site I'm running Smile My old code is a jungle - CI to the rescue!


Reoccuring code - El Forum - 07-02-2007

[eluser]Phil Sturgeon[/eluser]
Hmm, I have been wanting to reopen my old gaming site using CI for some time. That site has been down many years and once the base is done i have some slick ideas on how to make a fat gaming site. Drop me a line on MSN if you fancy a joint venture.


Reoccuring code - El Forum - 07-03-2007

[eluser]esra[/eluser]
You could build a library based on the use of the factory design pattern. A factory is composed of get and set (create) methods and is designed to delegate responsibilities to other libraries. You can then load the factory in your base contoller. The factory can delegate to other libraries without loading those libraries until they are delegated (called) by the factory.

You can find information about the Factory pattern on the web. For a good example used in another PHP MVC framework, look here:

http://api.joomla.org/Joomla-Framework/JFactory.html