I have this marked as PHP but only because I'll be using PHP code to show my problem.
So I have some code like this for the controller:
switch ($page)
{
case "home":
require "views/home.php";
break;
case "search":
require "views/search.php";
break;
}
Obviously there's more pages but this should illustrate my issue. There is a common header, navigation and footer for both of these pages (and for all pages on the site for that matter). Should I be using multiple require statements? My first guess would be:
switch ($page)
{
case "home":
require "templates/header.php";
require "templates/navigation.php";
require "views/home.php";
require "templates/footer.php";
break;
case "search":
require "templates/header.php";
require "templates/navigation.php";
require "views/search.php";
require "templates/footer.php";
break;
}
Somehow my gut tells me this isn't correct.
Model–view–controller (MVC) is a software architectural pattern commonly used for developing user interfaces that divide the related program logic into three interconnected elements.
The controller should just set up the data for the view and choose which view to display. The view should be responsible for the layout of the page, including shared pages. I like your first sample over the second.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With