tags:

views:

246

answers:

1

I'm new to the MVC concept, and somewhat new to PHP.

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?

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 (Kohana) setup?

Edit: Regarding Kohana v2.3.4

A: 

For Question 1 & 2, I would use hooks for these. Hooks allow you to have code executed at various times throughout the framework's startup and tear down phases. Checkout the documentation page on hooks on Kohana's site. The events documentation has all the events listed that you can use throughout the framework.

Question 3: You don't ever really have to use a constructor in your controllers. One reason to do it may be if you have several actions that need the same object instances and things like that, you can create them once in the constructor.

Chris Gutierrez
Why would several actions need the same object instances? :P Shouldn't the application state be maintained by the models?
helloworlder
Maybe I'm wrong but is there never an instance where several actions use the same model? I was mainly talking about the physical location of where to create the model instances.
Chris Gutierrez
Nah most likely I'm wrong -- I'm still pretty new to MVC so I just ask questions to learn more :-)Thx
helloworlder