Welcome Guest, Not a member yet? Register   Sign In
Beginner Q, how to run a function every page load?
#1

[eluser]Jeff_B[/eluser]
My first question! Note: up until yesterday, I did not know what "MVC" was. I think I have a decent understanding of it now.

Question 1
Before every controller is loaded, I'd like to run a function which checks to see if a database-table exists. Is the proper way to do that with hooks? Could you provide a short example?

Question 2
Before every controller is loaded, I'd like to set a few variables which are determined by a call to the db. How/where is that supposed to be done in a MVC (CodeIgniter) setup?

Question 3
In your controller, is it okay and/or normal practice to wrap the "$this->load->view" in a conditional? For example:

Code:
class Foo extends Controller {
    function index()
    {
        if ($xyz) {
            $this->load->view('fooview');
        }
        else {
            $this->load->view('barview');
        }
    }
}
#2

[eluser]Andrew Cairns[/eluser]
Hi Jeff,

I think the best solution would be to extend the base controller.
That will allow you to check for a db table upon page request and also assign your class attributes depending on your db query.

It is also fine to use conditional statements to determine which view to render.

Also, dont forget your constructor within your controller.
#3

[eluser]Jeff_B[/eluser]
[quote author="Andrew Cairns" date="1261508955"]Hi Jeff,

I think the best solution would be to extend the base controller.
That will allow you to check for a db table upon page request and also assign your class attributes depending on your db query.[/quote]

Hi Andrew, would you mind showing me an example of "extending my base controller?"
#4

[eluser]Colin Williams[/eluser]
http://codeigniter.com/wiki/Header_and_F...ge_-_jedd/
#5

[eluser]BrianDHall[/eluser]
For things that only need to run in a given controller, use your controller's Constructor. (the function named the same as the class name)

The MY_Controller concept is best for things that should run in all controllers.




Theme © iAndrew 2016 - Forum software by © MyBB