What is the correct (recommended method) for passing information to a view in a MVC environment.
Currently we use Zend Framework, where we write classes to开发者_如何学C handle specific things EG
a Book class with a save and load method to retrieve info from the DB, which is called from a particular nameAction().
What I would like to know is what is the best way to pass this information to the view, in some cases we do $this->view->book_name = $book->getBookName();
and in other cases we do the following
$this->view->book = $book; OR $this->view->books = Book_Manager::getAllBooks(); and then access the object(s) properties in the view.
Information on-line suggests we try limit what access a view has to information, i.e pass them only what they need and in some cases people say its ok to pass stuff through as long as nothing is done to that information.
Regards
I agree with the second viewpoint; I think it's fine to pass along entire objects to the view, so long as the template writer is unable or unlikely to mutate the object. There are several ways to ensure this, but on a small enough team, I'd hope a stern warning is enough =)
I usually tend to use the second form (passing the full object to the view) because, let's face it: it's easier to pass the object and then in the view to render the needed information.
As a good practice, I'd probably go for the first option: pass to the view only what's really needed, but in a world where we never stop developing, I vote for the second.
精彩评论