php - Custom MVC, how to implement a render function for the controller so the View can access variables set by the Controller -
i'm adding new features existing code base. anyway, current feature i'm doing should in mvc in opinion. existing code base isn't mvc feature i'm implementing, want mvc. , don't want roll existing mvc existing codes.
so, problem is... don't know implement render function controller class. usually, in mvc have controller stuff, set variable using render function, , view can magically access given variable set controller.
i have no idea how other global, feel wrong, keep on telling myself there has better way. edit: it's global isn't it? >_> how other frameworks it?
here's silly example:
controller:
class usercontroller extend basecontroller { public function actionindex() { $user = new user; // create instance user model $user->getlistofuser(); $this->render('listofuser', 'model'=>$model); } }
view:
<?php //i can use $listofuser now... //some loop echo $listofuser[$i]; ?>
thank in advance!
a simple example:
class view { function render($file, $variables = array()) { extract($variables); ob_start(); include $file; $renderedview = ob_get_clean(); return $renderedview; } } $view = new view(); echo $view->render('viewfile.php', array('foo' => 'bar'));
inside viewfile.php
you'll able use variable $foo
. code in view file have access $this
(the view instance) , variables in scope inside render
function. extract
extracts array's contents local variables.
Comments
Post a Comment