Welcome Guest, Not a member yet? Register   Sign In
Everywhere stuff
#1

[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!
#2

[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
#3

[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=
#4

[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.
#5

[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=




Theme © iAndrew 2016 - Forum software by © MyBB