(mvc theory) should a controller call *only* one view.php?

#1
[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.

#2
[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.

#3
[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.

#4
[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:
..

contactus.php
docs.php
formsuccess.php
hire.php
home.php
myform.php
products.php

_doctype_strict.php
_doctype_transit.php

_doc_faqs.php
_doc_links.php
_doc_privacy.php
_doc_sitemap.php
_doc_terms.php

_home_welcome.php

_com_analytics.php
_com_footer.php
_com_header.php
_com_menu_left.php
_com_pic_msg.php
_com_title.php

_hire_box.php
_hire_credit.php
_hire_cyclecarrier.php
_hire_dates.php
_hire_form.php
_hire_location.php
_hire_menu.php
_hire_vehicle.php
_home_products.php

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.
 

#5
[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.


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2021 MyBB Group.