php mvc在提交页面上发布带有提醒的帖子

I'm having a little problem in my own written MVC framework. When i post a form to another page i want to show a alert there.

For example: i have build a blog in my mvc framework. There are 3 controllers/methods in here: 'blog/overview', 'blog/addPost', 'blog/deletePost'. When i'm in the methode 'blog/addPost' it calls a view with a form like so:

<form action="blog/overview/" method="post">
  <input type="text" name="title" />
  <input type="text" name="post" />
  <input type="submit" name="addPost" />
</form>

As you can see i'm posting to the methode 'blog/overview'. When the form is posted and the blog is successful added, i want to be able to show an alert that says 'The blog post is succesful added.' in the 'blog/overview' view.

Does it mean that i must check in the methode 'blog/overview' if there was a post, and where it's from? Because i want to do the same thing when i delete a blog post. That means i have to check 2 things already in the 'blog/overview'. And it seems to me this is not the right way to do it.

Does someone please can tell me how this is done?

What I always do is check which button has been pushed so

   if(isset($_POST['addPost'])){
    //do something
    header("Location: /blog/overview/");
   }

What this does is it'll only run the code in the if statement if the addPost submit button was pushed. That way if you have two buttons on the form, you can run different code based off the button pushed. I hope that helps/answers your question.

Any (correct) submission of form follows the Post/Redirect/Get pattern, splitting it into two major parts (stages) that have to be applied to the larger MVC design pattern:

  • First stage: POST-REDIRECT

    The form should be posted with <form action="/blog/addPost" method="post">. This would call the addPost() method in the controller, which passes the necessary information to service in model layer, that is responsible for managing articles.

    Said service tried to save your article in whatever form of permanent storage you use. If this operation fails, service save the error state (usually in session). If operation is successful, the service stores "last operation" somewhere in session, to be recovered later.

    Then view, sees that there was a new change in model layer and redirect to /blog/overview by producing a response, which contains only HTTP location header.

  • Second stage: GET

    Controllers overview method is called, but only as courtesy.

    When view receives a command to produce response, it first request model layer (most likely, the same service for managing library of article), whether there has been an error state set.

    Service tries to recover the error state from session, and, if it was stored before, send back to view the error code.

    View assembles the HTML response for the overview, and, if model layer returned an error code, one of templates used for creating said HTML contains the fragment for displaying the error message.

    If there are no errors, view can request model layer for last change or last operation, which on successful post you can store in session too. This way view will know that there was some previous operation done, and it has to also add template for "successfully added/removed" message.

This would be the simplified step-by-step process. You have to understand that MVC and MVC-inspired patterns are made for providing a structure in complicated applications and you blog might be way too simple, thus choosing MVC design pattern might not be as pragmatic as you might imagine.