Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trial
Keith Kelly
21,326 PointsMVC
In the MVC video Randy explains that separating the controller code is an advanced application.
What I am wondering is why couldn't we create a separate controller php file (similar to what we did with the model) and call it with a require_once in the the head of the page?
2 Answers
Kevin Korte
28,149 PointsMy guess and understanding of it is doing it that way, you would have the view calling the controller, and instead you would want the controller calling the view, which becomes quite advanced to write the logic to do so.
The controller should call the right view, and populate the view with the right data from the model. A CMS example I can think of right away is wordpress. When you navigate to a post or page, Wordpress calls the right view (single.php, single-{custom-post-type}.php, home.php, etc) and populates it with data from the model, which is pulling out of the database. If Wordpress can't find the right view code, it's smart enough to fall back in a hierarchy format, all the way back to the index.php if it needs to.
Thats my understanding of it all.
Kevin Korte
28,149 PointsI believe so, and I think that is how WP accomplishes it. The nitty gritty starts to get heavy on the programming side, and I am not a programmer.
Keith Kelly
21,326 PointsThanks for the clarification!
Kevin Korte
28,149 PointsNo problem, wish I could be a little more clear. You might find it interesting to read through some of the core Wordpress files to see how one file calls another, and see if you can pick out what is the M-V-C code.
Keith Kelly
21,326 PointsKeith Kelly
21,326 PointsOK. That seems to make more sense.
If I am thinking of this correctly could you have an index page that, based on a page type returned, can load the view pages with a conditional?
For example: