1
votes

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?

1
Are you aware that your "post" is a monolithic wall of text with sentences spanning up to 5 lines. - tereško
You're right... @tereško just fixed that 4 hours ago... - Ali Sajjad

1 Answers

0
votes

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.