panique / huge

Simple user-authentication solution, embedded into a small framework.
2.14k stars 789 forks source link

ErrorController redeclaration #899

Open elbenjaz opened 2 years ago

elbenjaz commented 2 years ago

Hi all,

At this URL: {Base URL}/error/whatever

The following error will appear: "Fatal error: Cannot declare class ErrorController, because the name is already in use..." (due a double "require" in application/core/Application.php at L37 and L51).

My suggestion for fixing this issue is to change L51 in application/core/Application.php: (before) require Config::get('PATH_CONTROLLER') . 'ErrorController.php'; (after)require_once Config::get('PATH_CONTROLLER') . 'ErrorController.php';

Greetings

losttheplot commented 2 years ago

I'm using huge at the core of quite a large project but I don't have this issue because I have created a method within Redirect.php as follows:

    //----------------------------------------------------------------------------------------------
    // redirect the user to a 404 page, according to whether or not they are logged in
    //----------------------------------------------------------------------------------------------
    public static function error404()
    {
        if (Session::userIsLoggedIn()) {

            // if the user is logged in, load the application's 404 page (shows header-bar and side-menu)
            $config = Config::getInstance();
            require $config->getApp('path_controller').'access/ErrorController.php';
            $controller = new ErrorController;
            $controller->error404();

        } else {

            // if the user is not logged in, load the application's plain 404 page (only shows dialogue box)
            $config = Config::getInstance();
            require $config->getApp('path_controller').'access/ErrorController.php';
            $controller = new ErrorController;
            $controller->loggedOut404();
            exit();
        }
    }

...and then in Application.php I have...

            } else {
                // show a 404 error page if the requested method does not exist
                Redirect::error404();
            }
        } else {
            // show a 404 error page if the requested controller does not exist
            Redirect::error404();
        }
    }

Obviously my Error Controller also looks a little different because it now has two different methods for rendering two different views, but I'm sure you get the gist.

elbenjaz commented 2 years ago

Hi,

I also tried that!

(before) require_once Config::get('PATH_CONTROLLER') . 'ErrorController.php'; $this->controller = new ErrorController; $this->controller->error404();

(after) Redirect::to("error/error404"); Personally I prefer not to do a redirect so that the user does not "lose" the URL (similar to what Google, Amazon and many others do).

Regardless, both changes should be a reasonable solution to the issue :)