0

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?

Paul R
  • 208,748
  • 37
  • 389
  • 560
  • i've build a form validator as well but it seems that i have to do everything in the 'blog/overview' method. Just for showing a alert there. i really don't thing it's the right way to do it. – Michael van de Rijt Nov 21 '12 at 22:21

2 Answers2

1

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.

Ian Overton
  • 1,060
  • 7
  • 17
  • I know thats the code to check if a form is post. But do i check every form post in the method 'blog/overview'? – Michael van de Rijt Nov 21 '12 at 22:16
  • Yes. If you are building a framework though, if you standardize your vocabulary it might make sense to have a router/dispatcher to off load some of the controller responsibilities that are repeated. – Ian Overton Nov 21 '12 at 22:18
  • i'm using a router/dispatcher in my framework, but i don't see how this is gonna solve my problem? – Michael van de Rijt Nov 21 '12 at 22:22
  • The dispatcher could check for common form submits & do an action before it routes. For example if you are doing crud (create, read, update, delete) operations, but with a different object. It doesn't need to exist in the controller as duplicated code. In my framework to handle this problem I actually had the class that handles rendering the view/layout handle the crud controller operations, so in the blog/overview method I just had to call the method to render the page and pass in the model and it knew how to do crud operations and render the page. – Ian Overton Nov 21 '12 at 22:40
  • This sounds like a right way to solve my problem. Can you show me a example / tutorial how this is done? – Michael van de Rijt Nov 21 '12 at 23:00
  • I don't know how much you really want to see as this is starting to get into how building a whole mvc framework, but for a dispatcher method you'd probably route all the web traffic to a single file using htaccess rewrite or auto_prepend_file and build the crud functions there before it routes. Or you build a function like I said with the if statement I originally gave you along with the code for displaying all the html view files in a render view function. – Ian Overton Nov 22 '12 at 03:59
  • I've modified my answer to what I think your original question was. I would put that if statement in the add method and apon success set a session variable you can check for your message and redirect the user to the overview – Ian Overton Nov 22 '12 at 12:42
1

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.

tereško
  • 58,060
  • 25
  • 98
  • 150
  • So if i'm correct: i will post the form to 'blog/postAdd' this does call the model that will add the post to the database and returns a response to the controller. This response is stored in the session and after a succesfull add i will redirect to the 'blog/overview' view en read the session for alerts? – Michael van de Rijt Nov 21 '12 at 22:46
  • In correctly implemented MVC the controller actually never retrieves information from model layer. That is done by view instances (classes/object, which are responsible for UI logic). You could say that controllers "write" to model layer and view "read" from it. Model layer (which usually consists from services, domain objects and storage abstractions , like data mappers), save the state internally. Then view is tasked to produce the `overview`, it requests from model layer the information about that state. – tereško Nov 21 '12 at 22:48
  • What i mean is that the controller checks the submitted data if this is valid the model layer is called to add the post to the database. The model return true or false. If this is true the post is succesfull added. Then there will be a alert added to the session and the page will redirect to 'blog/overview'. Here i have to check if a alert is set in the session? Is this the right way to do it? – Michael van de Rijt Nov 21 '12 at 22:55
  • actually the validation of data should happen in [domain objects](http://c2.com/cgi/wiki?DomainObject)(those are what people usually mean, when they say "models"), which are part of model layer. The service (from model layer) shouldn't return anything to the controller. View should be requesting directly whether previous operation was successful or not. – tereško Nov 21 '12 at 23:12
  • i realy lost track here. Can you please show me example / tutorial to solve my problem? And is there a solutions to do it in the dispatcher/router like Ian Overton says down here? – Michael van de Rijt Nov 21 '12 at 23:28
  • Honestly, i think that Overton is talking bullsh*t. Routing mechanism does not share any responsibilities with controller. Routing mechanism can be part of something known as ["front controller"](http://martinfowler.com/eaaCatalog/frontController.html)(which is a really bad name for something that is not directly part of MVC), which is what people usually do in bootstrap, minus the autoloader setup. – tereško Nov 22 '12 at 00:09
  • I had post a question on a different forum as well, and here someone says i might looking for something called 'flash messenger'. i have searched it on the internet and i see it's something like you telled me, store messages in the session. – Michael van de Rijt Nov 22 '12 at 00:26
  • **about the tutorials:** no, there are no good tutorials/example for this in PHP. Most of stuff you can find are Rails-clones, which use MVC only for promotion and not for code. You might benefit from reading [this answer](http://stackoverflow.com/a/5864000/727208), but if you actually want to understand concepts behind MVC, you will have to read [**PoEAA book**](http://www.amazon.com/Patterns-Enterprise-Application-Architecture-Martin/dp/0321127420). Actually ... if you are trying to make a framework, it should be mandatory reading material. – tereško Nov 22 '12 at 00:31
  • **about flash messenger:** yes, that's kinda what you would be looking at, but if you are using MVC pattern, then the parts of messenger would be distributed all over the MVC triad: in model layer for storage, and in view for visualization. – tereško Nov 22 '12 at 00:34