Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Which Code Should Go Where in MVC Structure

My problem is in somewhere between model and controller.Everything works perfect for me when I use MVC just for crud (create, read, update, delete).I have separate models for each database table .I access these models from controller , to crud them . For example , in contacts application,I have actions (create, read, update, delete) in controller(contact) to use model's (contact) methods (create, read, update, delete).

The problem starts when I try to do something more complicated. There are some complex processes which I do not know where should I put them.

  1. For example , in registering user process. I can not just finish this process in user model because , I have to use other models too (sending mails , creating other records for user via other models) and do lots of complex validations via other models.
  2. For example , in some complex searching processes , I have to access lots of models (articles, videos, images etc.)
  3. Or, sometimes , I have to use apis to decide what I will do next or which database model I will use to record data

So where is the place to do this complicated processes. I do not want to do them in controllers , Because sometimes I should use these processes in other controllers too. And I do not want to put these process in models because , I use models as database access layers .May be I am wrong,I want to know . Thank you for your answer .

like image 922
Oguz Bilgic Avatar asked May 07 '10 03:05

Oguz Bilgic


People also ask

What goes in the model in MVC?

An MVC model contains all of your application logic that is not contained in a view or a controller. The model should contain all of your application business logic, validation logic, and database access logic.

Where does the main logic go in a basic MVC application?

A1: Business Logic goes to Model part in MVC . Role of Model is to contain data and business logic. Controller on the other hand is responsible to receive user input and decide what to do.

What is MVC architecture how it is organized?

Model–view–controller (MVC) is a software architectural pattern commonly used for developing user interfaces that divide the related program logic into three interconnected elements. This is done to separate internal representations of information from the ways information is presented to and accepted from the user.


2 Answers

Just a short comment (no solution) AFAIK that is an eternal question - MVC is just a pattern, and as such, is in theory implementable cleanly. In practise, due to limitations set by available tools (such as programming language library contents and UI component interface design..) you have to make local decisions. The important thing is that you aim to separate these...and not have everything in one mess. I take my comment off the air and am left to see if someone has a "final solution".

like image 166
Hubbard Avatar answered Sep 28 '22 07:09

Hubbard


For simple tasks I would write action helpers (e.g. sendNewsletter).

For sophistocated tasks I woud create services (eg. email, auth etc.).

like image 30
takeshin Avatar answered Sep 28 '22 09:09

takeshin