This is my first PhP-MySQL project and my first (ever) question. Am trying to make a small portal and in order to learn the basics I am trying to use the front controller pattern as I am not confident using the Observer pattern at present.
The front controller basically looks something like below and calls the appropriate method of the correct class:
$controller =''; $action =''; $queryString ='';
parseURL($controller, $action, $queryString);
$objView = new View;
if ($controller == 'adminlogin' && $action == 'authenticate') {
AdminLogin::getInstance()->authenticate();
} else if ($controller && $action) {
SessionFactory::getSession();
if (isset($_SESSION['userName'])) { // and some more validations
callHook($controller, $action, $queryString);
} else {
$objView->assign('message', SESSION_INVALID);
$objView->display('index');
}
} else {
$objView->display('index');
}
The view is also simple:
public function assign ($variable, $value)
{
$this->passArray[$variable] = $value;
}
public function display ($view)
{
$mapper = ViewmapSingleton::getinstance();
if (1 == preg_match("/\/|\.|\\\\/", $view)) {
echo 'View name should not contain . / or \ ';
}
$template = $mapper->getViewPath($view);
if (!$template || is_dir($template)) {
echo 'The requested view file does not exist';
} else {
extract ($this->passArray);
include_once($template);
}
}
My Problem is the Browser's BACK button when submitting $_POST forms. I am trying to make an Admin page that shows article listings and allows pagination, bulk actions, and 'search articles by title / category' etc.
From what I have read on this (most useful) website, there exist two solutions to prevent the Browser's back button from re-submitting a form:
Solution 1. Pass the search parameters to the action (method) by appending them to the URL using Javascript on the Search button.
<input id="btn1" type="submit" onclick="SubmitForm('index.php? controller=articles&action=showList&articleTitle='+document.getElementById('articleTitle').value)" value="Search" name="btn1"></input>
=> Not the best way because there could be many many parameters like article-category etc.
Solution 2. Do not pass the parameters and instead use $_POST data in the called method. Save everything in $_SESSION and then redirect using header() to a separate display method in the same class. Extract everything from $_SESSION in the display function and generate the view.
=>Not the best way because there can be many many parameters that will have to be stored in the Session and then extracted.
Is there a more elegant way to prevent the browser's back button re-submitting a form when using a front controller? I ask because when using the front controller pattern, $_GET may not be of much use especially where some bulk actions require DB changes and are of the type "Unpublish".
Please ignore my ignorance and help!
Thanks