Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1093697
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T23:51:45+00:00 2026-05-16T23:51:45+00:00

Can you help me fix the exception/error message handling in my control architecture script?

  • 0

Can you help me fix the exception/error message handling in my control architecture script?

First, let me post the actual script…

Note: some of the code’s indentation is a bit off, but I am uncertain how to fix it. I apologize.

class FrontController extends ActionController {

//Declaring variable(s)
private static $instance;
protected $controller;

//Class construct method
public function __construct() {}

//Starts new instance of this class with a singleton pattern
public static function getInstance() {
    if(!self::$instance) {
        self::$instance = new self();
    }
    return self::$instance;
}

public function dispatch($throwExceptions = false) {

    /* Checks for the GET variables $module and $action, and, if present,
     * strips them down with a regular expression function with a white
     * list of allowed characters, removing anything that is not a letter,
     * number, underscore or hyphen.
     */
    $regex  = '/[^-_A-z0-9]+/';
    $module = isset($_GET['module']) ? preg_replace($regex, '', $_GET['module']) : 'home';
    $action = isset($_GET['action']) ? preg_replace($regex, '', $_GET['action']) : 'frontpage';

    /* Generates Actions class filename (example: HomeActions) and path to
     * that class (example: home/HomeActions.php), checks if $file is a
     * valid file, and then, if so, requires that file.
     */
    $class = ucfirst($module) . 'Actions';
    $file  = $this->pageDir . '/' . $module . '/' . $class . '.php';

    if (!is_file($file)) {
        throw new FrontControllerException('Page not found!');
    }

    require_once $file;

    /* Creates a new instance of the Actions class (example: $controller
     * = new HomeActions();), and passes the registry variable to the
     * ActionController class.
     */
    $controller = new $class();
    $controller->setRegistry($this->registry);

    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
        }
    }
}
}

abstract class ActionController {

//Declaring variable(s)
protected $registry;
protected $module;
protected $registryItems = array();

//Class construct method
public function __construct(){}

public function setRegistry($registry) {

    //Sets the registry object
    $this->registry = $registry;

    /* Once the registry is loaded, the controller root directory path is
     * set from the registry.  This path is needed for the controller
     * classes to work properly.
     */
    $this->setPageDir();
}

//Sets the controller root directory from the value stored in the registry
public function setPageDir() {
    $this->pageDir = $this->registry->get('pageDir');
}

//Sets the module
public function setModule($module) {
    $this->module = $module;
}

//Gets the module
public function getModule() {
    return $this->module;
}

/* Checks for actionMethod in the Actions class (example: doFrontpage()
 * within home/HomeActions.php) with the method_exists function and, if
 * present, the actionMethod and displayView functions are executed.
 */  
public function dispatchAction($action) {
    $actionMethod = 'do' . ucfirst($action);
    if (!method_exists($this, $actionMethod)) {
        throw new FrontControllerException('Page not found!');
    }
    $this->$actionMethod();
    $this->displayView($action);
}

public function displayView($action) {
    if (!is_file($this->pageDir . '/' . $this->getModule() . '/' . $action . 'View.php')) {
        throw new FrontControllerException('Page not found!');
    }

    //Sets $this->actionView to the path of the action View file
    $this->actionView = $this->pageDir . '/' . $this->getModule() . '/' . $action . 'View.php';

    //Sets path of the action View file into the registry
    $this->registry->set('actionView', $this->actionView);

    //Includes template file within which the action View file is included
    require_once $this->pageDir . '/default.tpl';
}
}

class Registry {

//Declaring variables
private $store;

//Class constructor
public function __construct() {}

//Sets registry variable
public function set($label, $object) {
    $this->store[$label] = $object;
}

//Gets registry variable    
public function get($label) {
    if(isset($this->store[$label])) {
        return $this->store[$label];
    }
    return false;
}

//Adds outside array of registry values to $this->store array
public function addRegistryArray($registryItems) {
    foreach ($registryItems as $key => $value) {
        $this->set($key, $value);
    }
}

//Returns registry array
public function getRegistryArray() {
    return $this->store;
}
}

class FrontControllerException extends Exception {

public function errorMessage($error) {

    //If throwExceptions is true, then the full exception is returned.
    $errorMessage = isset($error) ? $error : $this->getMessage();
    return $errorMessage;
}
}

Now, the problem… If I enter a URL with a nonexistent module (in the following example “BLAH”)…


http://example.com/index.php?module=BLAH&action=frontpage

…I get not simply the error message “Page not found!” but the following error message…


Fatal error: Uncaught exception 'FrontControllerException' with message 'Page not found!' in /web/example.com/library/php/ControlArchitecture.php:45 Stack trace: #0 /web/example.com/index.php(30): FrontController->dispatch(false) #1 {main} thrown in /web/example.com/library/php/ControlArchitecture.php on line 45

Any ideas on why I do not simply get the “Page not found!” message (instead of the uncaught exception)? Any ideas on how to fix this behavior?

Thanks again!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T23:51:45+00:00Added an answer on May 16, 2026 at 11:51 pm

    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
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

How do I fix this error? I can't find anything to help me! Code
Who can help me to fix the following problem? Here is the issue: in
Sure it's a simple question, but I can't fix it, can somebody help me?
Can somebody please help me fix my code.? I can't see where I'm going
Anyone can help me? Trying to Add a Slow function with my smoothscroll and
Who can help me to translate this XML Schema pattern [0-9]+-([0-9]|K) to java regular
Who can help me with regular expression? I need to remove a part of
Who can help me make the following query work... Both tables have the fields
Hopefully someone can help here, it's incredibly frustrating! I have a couple of iOS
Perhaps you can help me find this in the docs. I'm using pound-quote to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.