How do I pass information my a controller to a vie

2019-09-09 18:11发布

问题:

Using PHP, If I have a model (a class) where I various queries, whatever I need, and in my controller, I use myModel = new CustomerModel(); and later in the controller, say I call myMyodel in the controller (I know looks like codeigniter but I am not using a framework) to:

$data['query'] = myModel.OrderByLastName();

how do I pass that $data['query'] to a view, a separate .php page?

I don't wan to echo anything from my controller.

Also, was hoping this design, the way I explained it makes sense. Or am I wasting time with the model class?

回答1:

Typically, you'd instantiate a view object:

$view = new View();

Pass it the info it needs():

$view->set($name1, $value1);
$view->set($name2, $value2);
...

Then invoke the view's renderer:

$view->render();


回答2:

The way Django works is the controller basically renders a template using a templating system. It passes the data in Contexts, like this:

data['query'] = myModel.OrderByLastName();

context = {'data': data['query']}

page = loader.get_template('folder/template.phtml')

return render_to_page(page, context)

roughly.

Obviously, you're writing your own system so you've got some room on exactly how you implement it. I don't know if that's exactly what you want, but it might give you a workable idea.