1

I am trying to understand and use the MVC without a framework, until now I clearly understand what is the role of Model, View and Controller and how are they made.

But I have one question. If the Controller is a class with methods for every action in thew View, and in that method we communicate with the Model and ask some data or send some data, and then send that data to the view that we choose to display. My question is how we call that method from view when we need to send some data from view to controller?

Let say that we have a page with all users and when we click one user we want information about him and we send his id with post or get and we have a UserController with a GetUserInformation method wich communicate with model send the id receive the information set it to the view and call view() to show the information.

How do we call this method from View when the client clicks that user?

We made another file between them and send the user to that file and in that file we instantiate a controller object and call the method? But if we do that how is that file called in MVC?

And there are a lot of example, like login, add a user and so on when we need to call a method from controller and send some data or just call it without send data.

The general idea is how we call a method from a Controller object on an action in html page?

Ali Sajjad
  • 3,589
  • 1
  • 28
  • 38
Serban Alexandru
  • 165
  • 2
  • 13

1 Answers1

0

Since you have multiple php posts, I will assume that you are referring to implementing MVC or MVC-inspired architecture in context of web applications.

First of all: controllers are not responsible for passing data from the model layer (not a "model class") to the current view. The controller in MVC is only responsible for altering the state of the model layer (and in extremely rare cases - the current view's state) based on the user's input.

The second thing that you have to understand is the request-response nature of php. The instances in your application do not live past the execution of the site's code.

As for the view, its task is to create a response, which gets sent to a user. The "user" for your web application is not a human. Instead, what actually gets the response is a browser. And the browser does not get "the view". It only receives the response that was produced by the view.

Therefore:

It is not possible, in correctly implemented MVC-based web applications, for a view to call methods on the controller because:

  • you are not interacting with a view, but with a response
  • the view and controller are both already destroyed

P.S: at the beginning you wrote "until now I clearly understand what is the role of model,view and controller and how are they made", but it is clearly untrue.

Jacob Beauchamp
  • 532
  • 5
  • 18
tereško
  • 58,060
  • 25
  • 98
  • 150
  • Now I have the impression I dont't understand anything anymore:)) I only start to check up MVC a few days ago and look on a lot sites and examples and they all say the same thing controller comunicate with model and view,make the connection between them,I think you are trying to explain me things to abstract and I am at begining in web application,I start only two months ago and I learn alone from internet and there you can't find all the things that happen in back and the essence of stuff,things are explain in general and they only says the basic stuff. – Serban Alexandru Jul 28 '14 at 12:21
  • Could you please try to explain me on a lowel level or indicate me some resources where I can understand all those importanted basic stuff that many people don't care about or don't say about?I really want to learn things the right way and understand all the proccess in back,becuase on internet things are explained mechanic and they seem so easy but I see that it isn't like this. – Serban Alexandru Jul 28 '14 at 12:22
  • Those are just a few sites that I found when I check MVC online and from where I learn and you can see there why I understand things that way,because they all say the same things and are made the same,they only say in general,and don't cover all the things,don't say the real thing they all concentrate on the thing I sayed to and thats why it seem so easy http://code.tutsplus.com/tutorials/mvc-for-noobs--net-10488 http://salopek.eu/content/28/create-a-simple-php-mvc-framework http://blog.oscarliang.net/simple-mvc-framework-tutorial-part-1/ https://www.udemy.com/blog/php-mvc-framework-tutorial/ – Serban Alexandru Jul 28 '14 at 12:27
  • As for study materials, I usually just point people to [this list of material](http://stackoverflow.com/a/16356866/727208) (because I am lazy). Also, maybe [this old post](http://stackoverflow.com/a/9685039/727208) turns out to be helpful. – tereško Jul 28 '14 at 13:20