Hey in my View class after I load my view file and render() it should I be accessing my data like:
$this->model;
or like:
$this->data['model'];
or in the render() method before I include the view file have something like:
$model = $this->data['model'];
// and then in my view file I can access the data by just calling it locally
$model->getSomething();
from within my view files?
The three ways work but I’m starting to think something like $this->data['model']; is the correct way to access data from within a view file.
Which is the correct way?
Thanks.
In correctly implemented MVC, the view is not a dumb template. It is an instance of a class, that is able to request data from model layer. And model is not a class or instance. It is a layer (MVC is made from two layers: presentation and model).
The views in MVC design pattern are responsible for all of the UI logic. This meas that they (if necessary) create the response from multiple template. In other case the only response might be a HTTP location header.
Basically when your view instance needs something, it would do something like:
In this case the service factory is shared between current view and controller, and makes sure that only single instance of
Libraryservice is instantiated. Thus providing the ability for both instances to interact with same state of model layer.