1) Do you use action controller routing
2) Do you use header redirects
I've used the latter in the past and the former a few times in the last while. Both have their difficulties from what I can tell.
Which do you prefer and why?
Cheers
Moderator: General Moderators
Example:Hockey wrote:Agreed. Thanks for the confirmation, thats all I was really looking for was a second opinion.
The problem I am experiencing is POST data persisting without a redirection. However I cannot say I have encountered a situation where a 'forward' (better term) is required or desired over a redirect. Can you give an example?
For instance, if creating a user, one might create the user, redirect (prevent duplicate posting) and handle the request normally. Having the front controller call the 'create user' action only to have that create user action forward to the main handler, doesn't make technical sense as the POST is persisted.
With a redirect this is solved.
Anyways, if you know of a specific example, that would help me a lot
Thanks
Touche. I'll have to think about that for a bitd11wtq wrote:Example:Hockey wrote:Agreed. Thanks for the confirmation, thats all I was really looking for was a second opinion.
The problem I am experiencing is POST data persisting without a redirection. However I cannot say I have encountered a situation where a 'forward' (better term) is required or desired over a redirect. Can you give an example?
For instance, if creating a user, one might create the user, redirect (prevent duplicate posting) and handle the request normally. Having the front controller call the 'create user' action only to have that create user action forward to the main handler, doesn't make technical sense as the POST is persisted.
With a redirect this is solved.
Anyways, if you know of a specific example, that would help me a lot
Thanks
User submits a contact form to your "processForm" action. You validate the data they sent but they forgot to fill out a form field. You then set some error messages and within the same request you just forward to the displayForm action again. No redirect is needed because you haven't processed the data yet. Asking the browser to redirect the user to the form again just seems a bit pointless if you can forward the action request yourself.
Code: Select all
/**
* Executes the send action
*/
public function executeSend()
{
try {
$swift = SwiftFactory::getInstance();
$message = new Swift_Message("[SWIFTMAILER] " . $this->getRequestParameter("subject"));
$sender = new Swift_Address(trim($this->getRequestParameter("email")), $this->getRequestParameter("name"));
$message->setReplyTo($sender);
$filename = "No file uploaded";
if ($this->fileUploaded)
{
$filename = $this->getRequest()->getFileName("attachment");
$this->getRequest()->moveFile("attachment", sfConfig::get("sf_upload_dir") . "/" . $filename);
$message->attach(new Swift_Message_Attachment(new Swift_File(sfConfig::get("sf_upload_dir") . "/" . $filename)));
}
$tpl = file_get_contents(sfConfig::get("app_mail_template"));
$tpl = str_replace(array("{message}", "{sender}", "{date}", "{attachments}"),
array($this->getRequestParameter("body"), $sender->build(), date("r"), $filename), $tpl);
if ($this->fileUploaded)
{
$message->attach(new Swift_Message_Part($tpl));
}
else
{
$message->setBody($tpl);
}
$sent = $swift->send($message, sfConfig::get("app_mail_to"), sfConfig::get("app_mail_from"));
@unlink(sfConfig::get("sf_upload_dir") . "/" . $filename);
if ($sent)
{
$tpl = file_get_contents(sfConfig::get("app_autoreply_template"));
$tpl = str_replace("{sender_name}", $sender->getName(), $tpl);
$message = new Swift_Message("Thanks for contacting me regarding Swift Mailer", $tpl);
$swift->send($message, $sender, sfConfig::get("app_mail_to"));
$swift->disconnect();
$this->redirect("contact/thanks");
}
else throw new Exception("Message not sent");
} catch (Exception $e) {
$this->getRequest()->setError("runtime", "The message could not be delivered. " .
"This may be a temporary problem with the server. Please try later.");
$this->forward("contact", "index");
}
}
/**
* Validates the send action
*/
public function validateSend()
{
$failed = false;
$name = $this->getRequestParameter("name");
if (!$name)
{
$this->getRequest()->setError("name", "The <em>Name</em> field must be completed.");
$failed = true;
}
$email = $this->getRequestParameter("email");
if (!$email)
{
$this->getRequest()->setError("email", "The <em>Email</em> field must be completed.");
$failed = true;
}
else
{
if (!strpos($email, "@"))
{
$this->getRequest()->setError("email", "The email address does not look valid. Please check the address.");
$failed = true;
}
}
$subject = $this->getRequestParameter("subject");
if (!$subject)
{
$this->getRequest()->setError("subject", "The <em>Subject</em> field must be completed.");
$failed = true;
}
$body = $this->getRequestParameter("body");
if (!$body)
{
$this->getRequest()->setError("body", "Please provide a message to send.");
$failed = true;
}
$file = $this->getRequest()->getFileName("attachment");
if (!empty($file))
{
switch ($this->getRequest()->getFileError("attachment"))
{
case UPLOAD_ERR_INI_SIZE: case UPLOAD_ERR_FORM_SIZE:
$this->getRequest()->setError("attachment", "The file exceeds the maximum size.");
$failed = true;
break;
case UPLOAD_ERR_PARTIAL:
$this->getRequest()->setError("attachment", "The upload did not finish. Please try again.");
$failed = true;
break;
case UPLOAD_ERR_NO_FILE:
$this->getRequest()->setError("attachment", "The file did not upload.");
$failed = true;
break;
case UPLOAD_ERR_OK: default:
$this->fileUploaded = true;
break;
}
}
return !$failed;
}
/**
* Handles errors in the send action validation
*/
public function handleErrorSend()
{
$this->forward("contact", "index");
}haha...your right. I jumped the gun, no excuses. Having spent half my coding life as a desktop developer and switching to web dev some 6 or years ago, I often find myself trying to find the similarities and differences to better understand. For whatever reason, the multiple events per HTTP request in a front controller (like Zend or my own) simply slipped my comprehention and I couldn't see it's practical use, but thats why I posted here. Two heads are always better than one, regardless of how bright one might be.d11wtq wrote:I agree that you don't have to build something following a documented pattern to every single source line. The very nature of design patterns has the ideaology that they are merely a guide/template to help you tackle common problems aster but you will commonly customize code to your own specific needs.
A front controller is a very loose term anyway. There's no set-in-stone approach to it (some people even do them almost entirely procedurally). They are useful though and I disagree with the comment about things like this being the creation of overzealous developers. Using a front controller makes it easy to centralize logic that would otherwise need to be manually drawn into individual page requests. Things like Routing (without mod_rewrite "index.php/some/fake/path/here"), autoloading, response handling, database access etc become easier to provide to each request. I'm not saying you need a front controller to do all that, but it's a heck of a lot easier to maintain.