php - Controller or View -
i have coded controllers call data model , present data view:
class projectviewmodel { public $user = null; // contains authentication levels etc public $projects = null; } class projectcontroller { //... public function listprojects() { $viewmodel = new projectviewmodel(); $viewmodel->user = $this->sessionrepository->getsession(); $viewmodel->projects = $this->projectrepository->projects(); return view::make( "viewname", $viewmodel ); } }
now in view:
<ul> <?php foreach( $model->projects $project ) { ?> <li> <?=$project->title?> <?php switch( $model->user->authentication->type ) { case authenticationtype::admin: | <button>edit</button> break; } ?> </li> <?php } ?> </ul>
you see in my way of doing things, person designing view decides shown based off of user's authentication... don't take how have made too, can think model returning projects property states whether can or cannot edit specific project... main idea there 1 boolean value states if can or cannot project.
my colleague has gone different approach, interesting because defining within controller, if "button" (which may or may not exist if view person decides show in different way) should shown or not:
class projectcontroller { //... public function listprojects() { $viewmodel = new projectviewmodel(); $viewmodel->user = $this->sessionrepository->getsession(); $viewmodel->projects = $this->projectrepository->projects(); $viewmodel->buttons = array( "editbutton" => array( "name" => "edit button", "show" => ( $viewmodel->user == authenticationtype::admin ) ? true : false ), "openprojectreportbutton" => array( "name" => "open project report", "show" => ( $viewmodel->user == authenticationtype::admin ) ? true : false ) ); return view::make( "viewname", $viewmodel ); } }
in view uses buttons have been pre-declared in controller:
<ul> <?php foreach( $model->projects $project ) { ?> <li> <?=$project->title?> <?php if( $project[ "editbutton" ][ "show" ] ) { ?> <button><?$project[ "editbutton" ][ "name" ]?></button> <?php } ?> </li> <?php } ?> </ul>
although understand why might think idea, controller taking on of work of view... in fact has gone far view have buttons.. designer may disagree with...
it means if view guy wants add button somewhere else, he's going have ask controller guy give him new button in array else... , after doing that, may say, it's not button, want show image instead...
am right in thinking wrong?
overview:
- a controller should fetch data model, , push view view display how wants display it... view should use data decide how , show on view... ( further more this, if worried admin button shown accidently, if click on button provided view engineer, doesn't matter.. user get's taken page loads controller insists actually, user not have access page... )
- the view guy should asking questions like, can user edit projects... rather have been given button can display view
what if view guy decided actually, design purposes, want show buttons user can't use... , provide message stating why can't use it... controller in colleagues example has provided list of buttons have ignored considering view engineer (artist/designer) decided actually, screw controller... want show button!
am right?
edit:: i've added new tags because i'm aware php , c# people have different approaches problems... , i'm interested in view asp.net users aswell.
proper mvc separation separating responsibilities appropriately:
- the model work, application "can do" part of thick model layer
- the view visualises state of model, i.e. what's going on in app, user (or other entities)
- the controller reacts events (input) , directs them appropriate actions make happen in model , refresh view if necessary; it's plumbing between model, view , rest of world
as such, it's none of controller's business has presentation. view not single .php html template. view can thick model layer , responsibility that's necessary produce useful output. view should directly talk model state information needs appropriate. pretty of code that's in controller belongs view.
Comments
Post a Comment