How the view communicate with controller in MVC? - model-view-controller

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?

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.

Related

Save data through the controller after an event has been fired in laravel

I'm allowing a user to upload a post which includes an image and some related information as well as select the promotion fee[bronze,silver or gold].In my /post controller i'm validating the request and then sending a request to another API[third party api for performing payments].The API takes the user phone number and the promotion fee selected and does the payment transaction.So i made the Transaction model[used to save details concerning the completed transaction]to dispatch a created event since i want to save the other post related information only if the payment is successful.
I've tried to create the listener for the TransactionCreatedEvent but i can't find a way to pass the post related data into the listener in order to save them. I also tried to use the closure by calling the listen method on the Event Facade inside the post controller to no avail.
Mh, i'm not quite sure if i undestood you question correctly, but in you case, you could either relate the Transaction with the post, or pass the Transaction and the Post Model to any event or to a job, and then do whatever you want with the data in the event handler or job

Alternate option for call a function in all controller in spring

I have a notification section in my user area in my project. For the users there is lot of pages actually and each page i need to show the notification on top but each page calling different controller function. So my question is there any option for me to get this notification list on each user pages without calling the same notification fetching function on all controller or on all request mapping function.

MVC: Where to trigger user registration emails

I am building an MVC application (using the Zend Framework).
When users first register, the applicaiton sends them an email. My question is, where should I trigger this email from? The model or the controller? My thoughts are as follows:
In some ways, the model makes sense, since sending a registration email is part of my business logic. Users must click the link in the mail to validate their email address.
But by putting it in the model, I am 'encumbering' the model. The model's registerUser action is then only useful within the context of an application that needs emails sent for every registration.
Instead, by triggering the email from within the controller, my controller would be a litter 'fatter', but my model a little more 'fine grained'.
I have written an email service which actually configures and sends the email, and I think this is a good design decision. I am really just asking where I should be calling this service from.
Your thoughts are most appreciated!
According to Zend Framework's definition of MVC, you should put send the email from the controller:
Controllers...decide which view to display based on the user's request.
Models, on the other hand, contain:
...basic functionality behind a set of abstractions.
An email may be considered a "view" in that it displays information to the user. It is the controller's job to activate this "view."
In my opinion, I would want this in the model, as I would consider this an assumed process of the create user method, rather than any specific interaction with the user making the request.
In other words, since I would always want this email sent, regardless of the source of the request, I would identify this as a natural byproduct of the create user action, similar to a record being saved in a database.
You might want to look into using something like NServiceBus to queue messages to be sent to your Email Service.
This way you can have NServiceBus subscribe to an event that occurs and omit any manual firing of the email service etc.
Ultimately you want a failsafe way of ensuring your messages get to the intended people. This kind of framework will greatly help you ensure that this happens.
Alternatively you could store the emails to be sent inside your database and have your email service check the database queue every x minutes for new emails to send and omit the need for triggering the email sending.
Again, doing it this way will ensure at the least that the emails get sent. Should the network go down or some other disruption occur during the sending of each email you can simply leave them in the queue until the network comes back up.

Should the model or controller be responsible for sending emails?

In a MVC web application, I often send emails.
I usually do it in the controller, as I load all my views from the controller (including email views).
Now, however, I have some code where the email sends from the model.
Which tier is email generally sent from? Does it matter? Does it need to be consistent?
A controller should ideally be like an operator that connects a view to a model. This either belongs in the model or service layer.
I would argue that this belongs in the Model layer only if you have a model object that is solely responsible for sending e-mails. You don't want to comingle presentation and logic, that's the whole point of separation of concerns in Model-View-Controller.
This type of logic should reside in a service layer. You could then use dependency injection to inject the service into the controller and call EmailSenderService.sendEmail();

User confirmation in MVP/MVC along with web solution

How does one handle a user confirmation when using MVP/MVC in a Web environment?
Presenter:
if (_view.AskUserForConfirmation())
{
// do something
}
In a winforms project this won't be any problem but in a web environment the confirmation answear will come one postback after the preseter code, how does one handle such a thing?
Basically you don't...
Asking confirmation is in pure MVP not a responsibility of the Presenter. The logic in the presenter is called after the confirmation. I can understand what you are trying to achieve here but it is not possible to go back to the user during a Postback, request confirmation (or some other data) and use the result on the point where you left the routine.
Either accept that it is not presenter logic and that the logic of the view asked the confirmation, or redesign your application to use multiple views of which one asks for confirmation (Wizard style).

Resources