Love Fuel?    Donate

FuelPHP Forums

Ask your question about FuelPHP in the appropriate forum, or help others by answering their questions.
ViewModel vs View
  • Hey guys, im reading the fuel docs, when i saw "viewmodel are optional", just wanna ask about what the difference between viewmodel and view? and whats the instances that i might use viewmodel instead of view? after few minutes of testing the two, i might say viewmodel is someway faster than view. so its making me confuse to use view... any opinions or thoughts about this? PS: Just started to learn this framework, and its great, i just need to read docs to learn more...
  • You use a viewmodel in two different cases:
    1) you want to abstract the view from the controller
    2) you want to abstract view logic from the controller An example of case 1: your controller just loads the viewmodel, while in the view model you make the decision to load a view for normal browsers, a view optimized for iPads, or a view optimized for smartphones. Your controller doesn't need to worry about the output specifics, it's hidden in the viewmodel. You can even decide to return json instead of html... An example of case 2: For a lot of views, especially forms, you need additional information besides the data dealt with in the controller. A great example are dropdowns. Instead of doing all model calls in your controller to populate the dropdowns in your controller (and polute the controller with non-relevant code), you do the model calls in the viewmodel.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

In this Discussion