CodeIgniter Forums
Everywhere stuff - 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: Everywhere stuff (/showthread.php?tid=19417)



Everywhere stuff - El Forum - 06-06-2009

[eluser]adx1[/eluser]
Hey guys,

Is there a place in CI where I can put code that should be executed on every page?

Also, I have two views, a header and a footer, that need to be loaded for every page. Is
there a way to load them all the time? Not a big deal, preference thing, just wondering.

Thanks very much!


Everywhere stuff - El Forum - 06-06-2009

[eluser]Dam1an[/eluser]
Look into MY_Controller (search the forums, there's plenty of posts on it)
You put all the common functionality in there, any anything you want executed on every page, you put in MY_Controller constructor. Then any controllers which extend this will execute that constructor.

You can also acheive the problems with the views using MY_Controller, or if you need something more advanced look into a template library. You could also use a master view with the header/footer, and just load the content in the middle


Everywhere stuff - El Forum - 06-06-2009

[eluser]dcheslow[/eluser]
There is nothing particularly special about MY_Controller (or any other controller), they are just subclasses of Controller. So you can simply create a class that inherits from Controller and then make more classes which inherit from your subclass. In fact, having a 'base controller' allows you to consolidate lots of similar code found in all your controllers.

A simple example: almost all my controllers need to create timestamps (when a record was created, edited, deleted, etc.), so I have a method in my BaseController class to create a timestamp in MySQL compatible format. This may prompt the question "why not just use 'now()' in the insert statement?" Answer: If I'm inserting multiple records (for example, a base record and multiple related records) then I'd like them all to have the same timestamp... but the inserts might span a second/minute/hour boundary. So I create a single timestamp in PHP and use it for all the related inserts.

A more complex example: my BaseController has a save() method which tries to read a matching record from the database and then calls either insert() or update(), which are implemented in each BaseController subclass. Note that, because of the way that CI dispatches controller/actions, all the controller classes need to live in the Controllers directory (not subdirectories). This is a small price to pay.

=dave=


Everywhere stuff - El Forum - 06-07-2009

[eluser]Colin Williams[/eluser]
Quote:There is nothing particularly special about MY_Controller

Well, the 'special' thing about it is that it will get loaded automatically because of the naming conventions. The other option is to require() the parent/global controller before declaring your sub-controller class.

Quote:A more complex example: my BaseController has a save() method which tries to read a matching record from the database and then calls either insert() or update(), which are implemented in each BaseController subclass.

This should be done in the Model.


Everywhere stuff - El Forum - 06-07-2009

[eluser]dcheslow[/eluser]
So true; everything DB related should be in a model... BUT, I often have business logic that needs to be performed on Save (e.g. email the administrator, upload files, scrape websites, convert images, etc.)... sooooo... (and I should have been more clear in my earlier post).... the controller asks the model if this is a new record, performs business logic associated with save and dispatches to the sub-controller either insert or update. The sub-controller then performs any business logic specific to insert or update (usually not) and then dispatches an insert/update to one or more models.

=dave=