(mvc theory) should a controller call *only* one view.php? - Printable Version +- CodeIgniter Forums (https://forum.codeigniter.com) +-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20) +--- Forum: Archived General Discussion (https://forum.codeigniter.com/forumdisplay.php?fid=21) +--- Thread: (mvc theory) should a controller call *only* one view.php? (/showthread.php?tid=3068) |
(mvc theory) should a controller call *only* one view.php? - El Forum - 09-07-2007 [eluser]gavincali[/eluser] I'm using my controller, called browse.php, to call only 1 view file, called browseview.php. This view file calls other view files from a folder called /includes... and sometimes the include calls and include...etc... My question: Should my controller (browse.php) be able to choose from the 12 or so view files to call directly -OR- should it only call it's mate-view (browseview.php), and let this mate-view act as a traffic-cop to the other view includes? thanks. (mvc theory) should a controller call *only* one view.php? - El Forum - 09-07-2007 [eluser]Crafter[/eluser] That would make perfect sense from a design point of view, as far as the logical grouping of your application components go. However, one thing stands in the way of adopting this approach: It is not easy to invoke a controller function from another (meaning the framework does not support it). Your controller also needs to implement your business logic, and sometimes the browse() controller may choose not to show the browseview. That would mean enforcing a browser redirect or implementing one of the many workaround to invoke controller functions across each other. I say choose from a pool of views, depending on application logic. (mvc theory) should a controller call *only* one view.php? - El Forum - 09-07-2007 [eluser]Jim OHalloran[/eluser] I usually let the controller pick the most appropriate view for the task it's trying to perform (e.g. if it needs to display an error, let the controller pick the error view, if we need a browse view, let the controller select that one instead). Once the controller has selected the most appropriate view, I let the view itself take care of things like including the most appropriate combinations of common header/footer/navigation, etc. Hope that helps. Jim. (mvc theory) should a controller call *only* one view.php? - El Forum - 09-07-2007 [eluser]John_Betong[/eluser] [quote author="gavincali" date="1189216822"] I'm using my controller, called browse.php, to call only 1 view file, called browseview.php. This view file calls other view files from a folder called /includes... and sometimes the include calls and include...etc... My question: Should my controller (browse.php) be able to choose from the 12 or so view files to call directly -OR- should it only call it's mate-view (browseview.php), and let this mate-view act as a traffic-cop to the other view includes? thanks. [/quote] Hi GavinCali, I am in the process of writing a new application and have the following files in my view folder. Code: .. As you may presume from the naming convention I have: 1. one controller for most of the files without a "_" prefix 2. files prefixed with "_xxxx" are assocated with the "xxxx.php" controller and view. 3. files prefixed with "_com" can be used with all views. This has worked well and especially since my editor automatically lists the files alphabetically. (mvc theory) should a controller call *only* one view.php? - El Forum - 09-08-2007 [eluser]esra[/eluser] [quote author="gavincali" date="1189216822"]My question: Should my controller (browse.php) be able to choose from the 12 or so view files to call directly -OR- should it only call it's mate-view (browseview.php), and let this mate-view act as a traffic-cop to the other view includes?[/quote] Browseview can be the default view loaded by your index() method. You could have other methods in your controller that load other views and use redirect() in URL helper to load them where appropriate. Read the CI user guide sections describing how the segments in your url allow you to route to a specific controller and a method within that controller. Read the section on routing to learn how you can create an alias (route) which automatically redirects to a certain controller/method/. This should get you through the initial learning curve. Once you get accustomed to using segments, it's much more efficient than using something like a case statement to load various views. EDIT: It's also possible for a controller to display multiple views at the same time on its own or via AJAX calls. For example, many IDEs allow you to search through all the files in a project using a Find in Files command. The search results are displayed in a separate view. It's possible to mimic the same sort of functionality with CI. |