public function add() { if (!isset($this->currentUser)) { throw new Exception("Not in session. Adding posts requires login"); } $pincho = new Pincho(); if (isset($_POST["submit"])) { // reaching via HTTP Post... // populate the Post object with data form the form $pincho->setNombrePincho($_POST["nombrePincho"]); $pincho->setDescripcionPincho($_POST["descripcion"]); $pincho->setPrecio($_POST["precio"]); $pincho->setCeliaco($_POST["celiaco"]); // The user of the Post is the currentUser (user in session) try { // validate Post object $pincho->checkIsValidForCreate(); // if it fails, ValidationException // save the Post object into the database $this->pinchoMapper->save($pincho); // POST-REDIRECT-GET // Everything OK, we will redirect the user to the list of posts // We want to see a message after redirection, so we establish // a "flash" message (which is simply a Session variable) to be // get in the view after redirection. $this->view->setFlash("Pincho \"" . $pincho->getNombrePincho() . "\" successfully added."); // perform the redirection. More or less: // header("Location: index.php?controller=posts&action=index") // die(); $this->view->redirect("Pincho", "view"); } catch (ValidationException $ex) { // Get the errors array inside the exepction... $errors = $ex->getErrors(); // And put it to the view as "errors" variable $this->view->setVariable("errors", $errors); } } // Put the Post object visible to the view $this->view->setVariable("pincho", $pincho); // render the view (/view/posts/add.php) $this->view->render("pinchos", "add"); }