Acs Posted March 27, 2008 Share Posted March 27, 2008 I have been reading a lot bout the mvc pattern and I have some ideas on how to apply this to php! The problem is, how do you guys treat the view? I have seen some examples of some frameworks about creating a blog system or something like that and how they load the view. What I miss to see is how would they work with a global view. By global view I mean the sites default layout. Normally just the central part of the website changes and not everything. How do you guys do it? Do you have set a global view which is then filled with the view of the current controller set? Thanks Quote Link to comment https://forums.phpfreaks.com/topic/98220-how-to-handle-the-view/ Share on other sites More sharing options...
dbo Posted March 27, 2008 Share Posted March 27, 2008 I'm banging out that documentation for my little framework right now. If all goes well I'll get it wrapped up tonight and thrown up online somewhere. That being said here is how I'm handling it in my framework: This would be in my Hello_Controller... Variables get loaded and specify the masterpage to be used (this is the global view you are speaking of). In this example I've defined a variable "view" which tells the masterpage which view to use for the content section. public function index() { $arrData = Array(); $arrData['title'] = "Hello World"; $arrData['view'] = "Hello_View"; echo $this->_getView("Masterpage/masterpage", $arrData, true); } This obviously calls the _getView method which handles the request. Basically we create the Template (view) object and set the variables that were passed. If the view is something we want to cache, we tell it so that it can be cached after it's rendered. Ultimately the view gets loaded passing in the data. public function _getView($strView, $arrData, $blnIsCacheable=false) { if( $strView != "" && file_exists(VIEW_DIR . $strView . ".php") ) { $objTemplate = new Template(VIEW_DIR . $strView . ".php"); $objTemplate->setAll($arrData); if( $blnIsCacheable ) { $objTemplate->setIsCacheable(true); } return $objTemplate->load(); } } So then over in my masterpage view that gets invoked I've got this: <html> <head> <title><?php echo $title; ?></title> </head> <body> <?php include_once VIEW_DIR . $view . ".php"; ?> </body> </html> Which in turn includes the "subview" that we want to use for the content. That view looks like this: <h1>Hello View</h1> <?php _doGetInstance('router')->_getModel('Hello')->getNews(); ?> Of course in the above I'm also calling a model to populate some additional data, but that's okay There is certainly a lot of cleanup work that I can do with this, and I'm sure smarter people than me are cringing looking at this code, but it seems to be getting the job done and having thrown this thing together in about a day and a half of piddling I'm fairly pleased. Quote Link to comment https://forums.phpfreaks.com/topic/98220-how-to-handle-the-view/#findComment-502574 Share on other sites More sharing options...
Acs Posted March 27, 2008 Author Share Posted March 27, 2008 Thanks for the quick reply! In my cms I am doing something similar to that (except I don't implement the mvc pattern) I have the template system that checks to see if what I set X to is a variable or a file and if it's a file it will include the files. So I have my main template <body> <div id="woman"></div> <div id="Content"> <div id="Content2"> <div id="logo"></div> <div id="logo_bocado"></div> <div id="menuHolder"> <div id="navcontainer"> <ul id="navlist"> <li><a href="#" title="Inicio" name="inicio" >Inicio</a></li> <li style="margin-left:10px;"><a href="#" title="Quem Somos" name="quemsomos">Quem Somos</a></li> <li style="margin-left:10px;"><a href="#" title="Staff" name="staff">Staff</a></li> <li style="margin-left:8px;"><a href="#" title="Produtos" name="produtos">Produtos</a></li> <li><a href="#" title="Galeria" name="galeria">Galeria</a></li> <li style="margin-left:-5px;"><a href="#" title="Informações" name="info">Informa�oes</a></li> <li style="display:none"><a href="paginas/construcao.html" title="Construcao" name="Construcao"></a></li> </ul> </div> </div> <!-- menuHolder --> <div id="imagens"> </div><!-- imagens --> </div><!-- Content2 --> <div id="paginaHolder"> <div id="paginas">{TEMPLATE_BODY}</div> <div id="cantos"> </div> </div> <div id="ftr"><a href="http://www.antoniocs.org" target="_blank">AntonioCS.org</a></div> </div><!-- Content --> </body> And then I use templateManager::setTemplateCode("body","path to file"); to replace {TEMPLATE_BODY} with the page I want to see. Of course this is all called in a non mvc compliant way. That's why I really want to have a good grasp of the mvc pattern so that I can quickly make a very small framework just to show some pages that I have already done for this cms Thanks again! Quote Link to comment https://forums.phpfreaks.com/topic/98220-how-to-handle-the-view/#findComment-502603 Share on other sites More sharing options...
dbo Posted March 27, 2008 Share Posted March 27, 2008 Well I should have this code up soon, hopefully it will help, but I don't claim to be an expert. Quote Link to comment https://forums.phpfreaks.com/topic/98220-how-to-handle-the-view/#findComment-502622 Share on other sites More sharing options...
Acs Posted March 27, 2008 Author Share Posted March 27, 2008 Neither do I! All the help is appreciated Quote Link to comment https://forums.phpfreaks.com/topic/98220-how-to-handle-the-view/#findComment-502623 Share on other sites More sharing options...
activeserver Posted March 28, 2008 Share Posted March 28, 2008 I'm no expert, but I'll just add my opinion, fwiw. I prefer to think that a "routing table" is a good idea for an MVC architecture. Actually it should be called CMV because the order is C -> M -> V , but that's another topic So, further detailing it, we get this: User HTTP-Req + Data -> Routing Table (first lookup) -> Model / Business Logic / Database access -> Routing Table (second lookup) -> View / HTTP Response First lookup of Routing Table answers this question: "Which is the right business logic component or class or script to handle this form and data?" Second lookup of Routing Table answers this question: "Which is the right view component or class or script to display this form and data? And also, what further input form or interactive element should be shown to the user?" open to discussion Quote Link to comment https://forums.phpfreaks.com/topic/98220-how-to-handle-the-view/#findComment-502894 Share on other sites More sharing options...
activeserver Posted March 28, 2008 Share Posted March 28, 2008 Maybe this helps explain what I am trying to say.... http://imagebin.ca/view/oMmP1Mh.html comments welcome Quote Link to comment https://forums.phpfreaks.com/topic/98220-how-to-handle-the-view/#findComment-502902 Share on other sites More sharing options...
Acs Posted March 28, 2008 Author Share Posted March 28, 2008 A little bit complex your graphical representation. But I think what I should do is just use like a global view and the sub views. Which something I am using now in my little cms. I have seen someone using a router class in this tutorial and I was following it, but.. this router class seems something that the main controller could be. At least in my way of seeing this. Quote Link to comment https://forums.phpfreaks.com/topic/98220-how-to-handle-the-view/#findComment-502921 Share on other sites More sharing options...
dbo Posted March 28, 2008 Share Posted March 28, 2008 I've uploaded my generic framework. Anyone who was interested in seeing it can send me a PM and I'll send a you a link. I also appreciate any bug fixes or new features. Quote Link to comment https://forums.phpfreaks.com/topic/98220-how-to-handle-the-view/#findComment-503331 Share on other sites More sharing options...
activeserver Posted March 28, 2008 Share Posted March 28, 2008 A little bit complex your graphical representation ... this tutorial ... This is a page where controller routing is well explained: http://phpfuse.net/wiki/index.php?title=FuseURIRouter In general, IMO, we PHP programmers must stay in touch with the latest programming tricks (aka "patterns") in other languages or platforms, because PHP has some advantages which those do not and this extra power of PHP when combined with the well-organised "patterns" can create auick solutions. In particular, IMO, everyone interested in this MVC thing must read about "Struts" xml configuration files, "Model2", and "fat controller problem". Really nice stuff. HTH someone Quote Link to comment https://forums.phpfreaks.com/topic/98220-how-to-handle-the-view/#findComment-503535 Share on other sites More sharing options...
activeserver Posted March 28, 2008 Share Posted March 28, 2008 more: http://www.objectsource.com/ Especially: http://www.objectsource.com/Struts_Survival_Guide.pdf Page 24, Fig 1.3 Page 27 Fig 1.4 Enjoy! Quote Link to comment https://forums.phpfreaks.com/topic/98220-how-to-handle-the-view/#findComment-503537 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.