LemonInflux Posted June 6, 2008 Share Posted June 6, 2008 Hello. I've currently been looking into MVC for some software I'm writing. However, a couple things have stumped me. What does each thing actually do? As I understood it, view took care of templates and layout, controller did content and dynamic elements, but then there's no model. So can someone give me a human definition for each? Thanks in advance Quote Link to comment https://forums.phpfreaks.com/topic/109006-solved-mvc-help/ Share on other sites More sharing options...
stuffradio Posted June 6, 2008 Share Posted June 6, 2008 View = What you see Model = fetches database information, etc. Controller = Controls what happens Quote Link to comment https://forums.phpfreaks.com/topic/109006-solved-mvc-help/#findComment-559447 Share on other sites More sharing options...
LemonInflux Posted June 6, 2008 Author Share Posted June 6, 2008 so if I have, say, a gallery page... view would fetch the template, css, etc. etc. model would collect the photos, info etc. etc. controller would stick the two together? Is that right? Quote Link to comment https://forums.phpfreaks.com/topic/109006-solved-mvc-help/#findComment-559449 Share on other sites More sharing options...
stuffradio Posted June 6, 2008 Share Posted June 6, 2008 Correct me if I'm wrong but I believe so. Quote Link to comment https://forums.phpfreaks.com/topic/109006-solved-mvc-help/#findComment-559504 Share on other sites More sharing options...
mithras Posted June 7, 2008 Share Posted June 7, 2008 so if I have, say, a gallery page... view would fetch the template, css, etc. etc. model would collect the photos, info etc. etc. controller would stick the two together? Is that right? The model is in fact "stupid" and cannot do any actions on other objects. Your view is your template. The model just describes what the element contains: class photo{ var $name; var $id; var $imageLocation; } class album{ var $id; var $name; var $photos = array(); //Array of photo objects } class galleryController{ public function getAlbum( $id ){} public function displayAlbum( $id ){ $album = $this->getAlbum( $id ); $template = new template(); $template->assign( 'album', $album ); echo $template->display( 'album.template.php' ); } public function addPhotoToAlbum( $photo, $album) {} } Here you define two models: a photo and an album. The controller galleryController controls both of them (actually you need for the perfect design two controllers, but this is ok). The template 'album.template.php' is your view. Quote Link to comment https://forums.phpfreaks.com/topic/109006-solved-mvc-help/#findComment-559815 Share on other sites More sharing options...
LemonInflux Posted June 7, 2008 Author Share Posted June 7, 2008 So view is, obviously, the template and such. Controller controls what happens. Does the controller 'populate the model', if that makes sense? Sort of, fill in the blanks? I'm a bit confused :S Quote Link to comment https://forums.phpfreaks.com/topic/109006-solved-mvc-help/#findComment-559913 Share on other sites More sharing options...
Aeglos Posted June 7, 2008 Share Posted June 7, 2008 If I may add my two cents, since I struggled with the same a few weeks ago. After some reading and research I came to the conclusion that there are many "ways" to implement MVC. To name one for example, Codeigniter goes away with the model and integrates it into the controller, which is a departure from the traditional MVC implementation. The traditional way (and what I chose to use) is the "Fat model thin controller" approach. The model contains all your application logic, all your procedures, database interactions, validation, data manipulation, preparing and delivering. The controller is just an elaborate wrapper over your model; deppending on what page you are visiting (controller action) it decides which model methods to call, what data to pass to them, what to do with the data returned, which template to render, where to redirect, etc. It "controls" the application flow. I believe the controller should contain nothing more that very light validation (check if logged in for example) and simple control statements (if -then -else). The model is the true core of your application which spits data ready for your controller to pass to the View as-is with no further modifications. In fact, aside from setting a few variables with model or user input data and pass them around, the controller should touch nor manipulate no data at all. That's the model's job. This way you can create for example a Gallery Model with methods such as fetchById(), addPictureToGallery(), removePictureFromGallery(), deleteGallery(), createSubGallery(), etc. The controller then calls this model methods with GET or POST arguments and the passes the results to the View. If you then create another site which also uses a gallery, you can use the same model (and DB info most likely)... since it does not care where the data comes from it will work fine as long as you supply it in the correct format. But the new controller may get the arguments from POST, from SESSIONS, from another model, wherever. Hope it helps. And if anyone has something to add or correct please do, I'm far from being an expert in MVC. Cheers. Quote Link to comment https://forums.phpfreaks.com/topic/109006-solved-mvc-help/#findComment-560055 Share on other sites More sharing options...
LemonInflux Posted June 7, 2008 Author Share Posted June 7, 2008 So, from what I understood... Model has the tools, controller uses them? The controller just picks the bits from the model it needs? Quote Link to comment https://forums.phpfreaks.com/topic/109006-solved-mvc-help/#findComment-560062 Share on other sites More sharing options...
stuffradio Posted June 7, 2008 Share Posted June 7, 2008 These videos should help you http://railsenvy.com/2008/6/3/mvc-videos Quote Link to comment https://forums.phpfreaks.com/topic/109006-solved-mvc-help/#findComment-560113 Share on other sites More sharing options...
LemonInflux Posted June 8, 2008 Author Share Posted June 8, 2008 Haha, great, now I understand it! Fat model, thin controller, stupid view Strange how well they work... Thanks loads! Quote Link to comment https://forums.phpfreaks.com/topic/109006-solved-mvc-help/#findComment-560315 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.