I have a question about some MVC design and form submission. I have some problem when I want to save a resource.
For example I have a controller name vehicle
with the given methods :
vehicle
|-- list
`-- GET - Display the list of vehicle.
|-- add
|-- GET - Display the form for adding a vehicle.
`-- POST - Save the added vehicle.
My problem is that after the user has saved a vehicle, I want to go back to the list of vehicle displaying a notification. view_model.add_notification
is a method that displays a notification on the view.
a) I redirect the user to vehicle/list?success=1
and I call view_model.add_notification
when I see the success
in the query string
problem: the URL will contains the success=1
.
b) Inside the POST
request vehicle/add
I display the list after saving the vehicle and call view_model.add_notification
.
problem: the URL will be vehicle/add
c) I changed my controller to this
vehicle
|-- list
`-- GET - Display the list of vehicle.
`-- POST - Save the added vehicle.
|-- add
|-- GET - Display the form for adding a vehicle.
So I can call view_model.add_notification('success')
and then display the list and the URL will be fine.
problem: I feel this is not correct design to put the saving code in the list
method.
Please, can you have some input for me on how is the best way to design that ? To sum up the proposition I wrote:
a) bad url
b) bad url
c) bad design
Just set a session var like
$_SESSION['success'] = true;
and redirect the user to the list page. Then you can
if( isset($_SESSION['success']) ) {
// Do what you want to do when success is set
unset($_SESSION['success']);
}