Welcome Guest, Not a member yet? Register   Sign In
The right way to make it in mvc
#7

(This post was last modified: 05-23-2016, 08:57 AM by Andy N.)

(05-23-2016, 03:14 AM)InsiteFX Wrote:
(05-22-2016, 11:03 PM)PlorntXhizors Wrote: Can i use one model, controller for all my view files? Like functions.php in wordpress?


You should use one model per database table,

Controllers should be specific to the task and views should be one view for that task.

If you need a lot of code methods place them into a helper file.

Pretty much what he said.  If you try to lump everything into one model and one controller you're missing out on much of the benefits of the MVC approach.

The way I use MVC is that Models normally represent 'objects' (for example "Customers") which relate to one or sometimes multiple tables (for example "Orders" will typically have an order header table, and an associated order lines table, but you would only want one model).  The model provides a unified, re-usable way to interact with those objects - the most common cases being providing methods to create, read, update and delete them, aka CRUD.   They can also implement business logic.

Controllers typically encapsulate the processes / logic which defines the application, or at least that part of it which interacts with the user.  

Views define the user interface, and should be as 'dumb' as possible.

Some golden rules (or at least, rules I tend to follow):

- Views should be as logic free as possible, maybe some foreach loops and if statements but mostly just HTML with interpolated variables.
- Controllers should never interact directly with the data, that's what models are for.
- Sometimes it's not clear whether logic should be in the Model or the Controller. In general I would advise that logic that is specific to one use-case should be in a controller, whereas logic which is likely to be reused should be in a model.  

If you are trying to do something truly simple and disposable, you can just not bother with a model and do everything with just views and a controller.  Though I would personally tend to have a model just for the sake of good form.
Reply


Messages In This Thread
RE: The right way to make it in mvc - by skunkbad - 05-20-2016, 09:13 AM
RE: The right way to make it in mvc - by Wouter60 - 05-20-2016, 10:21 AM
RE: The right way to make it in mvc - by cartalot - 05-20-2016, 11:41 AM
RE: The right way to make it in mvc - by InsiteFX - 05-23-2016, 03:14 AM
RE: The right way to make it in mvc - by Andy N - 05-23-2016, 08:56 AM



Theme © iAndrew 2016 - Forum software by © MyBB