MVC > Where is best place to set view variables?

Not for 'how-to' coding questions but PHP theory instead, this forum is here for those of us who wish to learn about design aspects of programming with PHP.

Moderator: General Moderators

Post Reply
User avatar
MindOverBody
Forum Commoner
Posts: 96
Joined: Fri Aug 06, 2010 9:01 pm
Location: Osijek, Croatia

MVC > Where is best place to set view variables?

Post by MindOverBody »

Well, i am beginner in this whole MVC design pattern so I am bit confused about where is best place to set View variables, inside controller or a model? I read a few tutorials, in some of them variables are set in controller, in some are set inside model.
Which one is the best practice?
Last edited by MindOverBody on Wed Feb 16, 2011 11:42 pm, edited 2 times in total.
User avatar
Darhazer
DevNet Resident
Posts: 1011
Joined: Thu May 14, 2009 3:00 pm
Location: HellCity, Bulgaria

Re: MVC > Where is best place to set view variables?

Post by Darhazer »

model should not know anything about the view. Model should not set variables to the view
View have to retrieve variables from the model.
In most implementations, controller set the variables to the view, but I think that in the classic MVC, view retrieves the data from the model.
User avatar
MindOverBody
Forum Commoner
Posts: 96
Joined: Fri Aug 06, 2010 9:01 pm
Location: Osijek, Croatia

Re: MVC > Where is best place to set view variables?

Post by MindOverBody »

Thanks for your reply. It was helpful.
alex.barylski
DevNet Evangelist
Posts: 6267
Joined: Tue Dec 21, 2004 5:00 pm
Location: Winnipeg

Re: [SOLVED]MVC > Where is best place to set view variables?

Post by alex.barylski »

The model should always remain ignorant of the controller or the view, it has enough dependencies anyways.

In some web-based MVC implementations (the path I originally took) the controller is responsible for:

1. Querying the model, passing in POST, GET,COOKIE parameters.
2. Initializing the correct view(s) with the data returned by the model.

You lose some reusability with this approach and so many who have tried this approach, begin to implement a read-only dependency on the model from the view as well.

The model is injected into the view and the view is then responsible for directly querying the model. Never insert, update or delete, just select type queries. A pager is a good example of when this design approach becomes handy.

Cheers,
Alex
User avatar
MindOverBody
Forum Commoner
Posts: 96
Joined: Fri Aug 06, 2010 9:01 pm
Location: Osijek, Croatia

Re: [SOLVED]MVC > Where is best place to set view variables?

Post by MindOverBody »

Thanks Alex, your post helped a lot.
User avatar
MindOverBody
Forum Commoner
Posts: 96
Joined: Fri Aug 06, 2010 9:01 pm
Location: Osijek, Croatia

Re: [SOLVED]MVC > Where is best place to set view variables?

Post by MindOverBody »

Well, i designed some simple Controller, Model, View relation, so i am wondering is this correct. I took your advices and designed this little sample to check with you guys if i understood it right way.

Here is the code, it is well commented so you'll be able to check it in a sec :] :

Code: Select all

      class UserController {
	     // Controller properties
		public $model;
		public $view;
		
		// Controller constructor, this should be set in some
		// abstract controller class, aslo to load proper model
		// for controller
		public function __construct(){
			$this -> model = new UserModel;
			$this -> view = new View;
		}
		
		// In this method, all view variables are set
		// from model
		public function listAction(){
			$this -> view -> registerVar( "List", implode( ", ", $this -> model -> getList() ) );
		}
		
		// This method retuns final view object's html
		public function getResult(){
			return $this -> view -> getHTML();
		}
	}
	
	// Model for User Controller
	// This class also uses abstracted constructor where database
	// connection is set, and also destructor where database
	// connection is closed
	class UserModel {
		// This is simple method which returns data array from emulated 'database' record
		public function getList(){
			return array( "John", "Becky", "Ann", "Christopher", "Thomas" );
		}
	}
	
	// This is class where result of view templates is being made
	// With this class proper view is loaded and variables are set
	class View {
	     // Var stack
	     private $vars;
	     // Representation of loaded view template
		private $contentsOfViewFile = "Here is the list: <br/>{@List}";
		
		// This method paste variables into template and at
		// the end returns final HTML
		public function getHTML(){
		     foreach( $this -> vars as $key => $val ){
		          $this -> contentsOfViewFile = str_replace( "{@" . $key . "}", $val, $this -> contentsOfViewFile );
			}
			return $this -> contentsOfViewFile;
		}
		
		// This method is for view variable registration
		public function registerVar( $name, $val ){
			$this -> vars[$name] = $val;
		}
	}
	
	// This is nothing more than simplified representation of
	// Router object returned path
	$router["Controller"] = "UserController";
	$router["Action"] = "list";

	// This is what happens in dispatcher class
	$targetedController = new $router["Controller"];
	call_user_func( array( $targetedController, $router["Action"] . "Action" ) );
	
	// This should be echoed with some final Layout object
	// Where language is being aded, aslo HTML <head></head> is being set,
	// CSS loaded, etc.
	echo $targetedController -> getResult();
As you can see, I used simple 'templating engine' "View class" to render some template file. It is okay to use it this way? Duno' is it my OCD or what, but I love to have clean HTML without PHP tags in my view files.

I also designed this little flowchart of MVC advanced website. I am interested in your opinion and your experiences with this kind of design if anyone had any?
Attachments
MVC website flowchart
MVC website flowchart
CMS MVC.png (74.49 KiB) Viewed 5168 times
Post Reply