The error message actually says it all.
Wether the FrontController->dispatch()
methods argument is true
or false
the exception will be thrown anyway..(if there's some framework magic going on, please let us know which framework you're using)
So make sure you're catching the exception where you're calling it:
/* ... */
try {
FrontController->dispatch(false);
} catch (Exception $ex) {
echo "Eception caught: " . $ex.getMessage();
}
/* ... */
Update:
Here you can read about exceptions in PHP and how to catch them.
About the non-existent module issue:
$regex = '/[^-_A-z0-9]+/';
$module = isset($_GET['module']) ? preg_replace($regex, '', $_GET['module']) : 'home';
$action = isset($_GET['action']) ? preg_replace($regex, '', $_GET['action']) : 'frontpage';
$class = ucfirst($module) . 'Actions';
$file = $this->pageDir . '/' . $module . '/' . $class . '.php';
if (!is_file($file)) {
throw new FrontControllerException('Page not found!');
}
The IF-Statement
only checks if the module file (Pattern: ModuleActions.php) in this case BLAHActions.php exists or not. Once it has thrown the exception, your call will the cancelled and it won't be processed anymore. (This means that it won't even continue to check the Action
parameter)
About the non-existent action issue:
As of what I understand from the posted code, folowing method:
public function dispatchAction($action) {
$actionMethod = 'do' . ucfirst($action);
if (!method_exists($this, $actionMethod)) {
throw new FrontControllerException('Page not found!');
}
$this->$actionMethod();
$this->displayView($action);
}
makes calls to the required action (Pattern: doSomething) in this case doFrontPage isn't even called because your code throws an exception beforehand.
Calling a non existent action does not throw an unhandled exception, because it is handled
in your FrontController->dispatch() method just after the module check:
try {
//Trys the setModule method in the ActionController class
$controller->setModule($module);
/* The ActionController dispatchAction method checks if the method
* exists, then runs the displayView function in the
* ActionController class.
*/
$controller->dispatchAction($action);
}
catch(Exception $error) {
/* An exception has occurred, and will be displayed if
* $throwExceptions is set to true.
*/
if($throwExceptions) {
echo $error->errorMessage($error); //Full exception echoed
} else {
echo $error->errorMessage(null); //Simple error messaged echoed
}
}