We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 here.

How to custom a Exception page?

How to custom a Exception page , with css, js and with the messages that previous page throws ? (says in other way: How to beautify the exception page?)



58.4k

Hey man

First you need register exception for this we have many options for it, for example Registering it into dispatcher

$di->set('dispatcher', function () {
            $eventsManager = new EventsManager();
            $eventsManager->attach("dispatch", function ($event, $dispatcher, $exception) {
                //controller or action doesn't exist
                $object = $event->getData();
                if ($event->getType() == 'beforeException') {
                    switch ($exception->getCode()) {
                        case Dispatcher::EXCEPTION_HANDLER_NOT_FOUND:
                        case Dispatcher::EXCEPTION_ACTION_NOT_FOUND:
                            $dispatcher->forward([
                                'controller' => 'errors',
                                'action'     => 'index'
                            ]);
                            return false;
                        case Dispatcher::EXCEPTION_CYCLIC_ROUTING:
                            $dispatcher->forward([
                                'controller' => 'errors',
                                'action'     => 'show404'
                            ]);
                            return false;
                    }
                }
            });

And created a controller name errors and action index or anthing you want to create it



31.3k

Thanks!

But I think it can resolve the dispatcher error, like handler not found, action not found etc.

How to do with the system exception, like:

if (true) {
    throw new Exception("Error message");
}

then the "Error message"should displayed in a custom page.