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

Redirecting to default 'Error page' on PhalconExceptions

Hi all,

On user trying to access not existing urls I get pages with:

PhalconException: **xxxxx**Controller handler class cannot be loaded.

or

PhalconException: Action **xxxxx**was not found on handler '**yyyyyy**'

Any suggestion how to configure phalcon so on every not existing url phalcon instead of error message redirect to Error page (ErrorControler)?

Thanx, Jagg

edited Jul '14

Tried:

$di->set('router', function () {
        $router = new \Phalcon\Mvc\Router();

        $router->notFound(array(
            "controller" => "index",
            "action" => "route404"
        ));
        return $router;
});

And still same problem. Also by adding those lines, for first page, now I need to type

https://localhost/phalcon/index

instead

https://localhost/phalcon/

as before. Now https://localhost/phalcon/ redirects to route404.

Jagg



2.0k
Accepted
answer

Maybe its a bit better solution:

        //Registering a dispatcher
        $di->set('dispatcher', function () {
            //Create/Get an EventManager
            $eventsManager = new \Phalcon\Events\Manager();
            //Attach a listener
            $eventsManager->attach("dispatch", function ($event, $dispatcher, $exception) {
                //controller or action doesn't exist
                if ($event->getType() == 'beforeException') {
                    switch ($exception->getCode()) {
                        case \Phalcon\Dispatcher::EXCEPTION_HANDLER_NOT_FOUND:
                        case \Phalcon\Dispatcher::EXCEPTION_ACTION_NOT_FOUND:
                            $dispatcher->forward(array(
                                'controller' => 'index',
                                'action' => 'notFound'
                            ));

                            return false;
                    }
                }
            });

            $dispatcher = new \Phalcon\Mvc\Dispatcher();
            //Set default namespace to backend module
            $dispatcher->setDefaultNamespace("Baseapp\Backend\Controllers");
            //Bind the EventsManager to the dispatcher
            $dispatcher->setEventsManager($eventsManager);

            return $dispatcher;
        });


37.0k
edited Jul '14

@Ak-Army

How could I implement your solution into my code:


/**
 * Dispatcher use a default namespace
 */
$di->set('dispatcher', function () {
    $dispatcher = new Dispatcher();
    $dispatcher->setDefaultNamespace('Vokuro\Controllers');
    return $dispatcher;
});

Could you help?

Thanx Ak-Army ;)



2.0k
edited Jul '14

@Flanex:

Just copy-paste the code into your, and change the forward controller and action and the default namespace.



37.0k

@Ak-Army

Thanks, Ak-Army ;)

If somebody is interested, below is the code that is working for me (don't forget to create the notFound action in your index controller otherwise you will be getting cyclic error).

 //Registering a dispatcher
        $di->set('dispatcher', function () {
            //Create/Get an EventManager
            $eventsManager = new \Phalcon\Events\Manager();
            //Attach a listener
            $eventsManager->attach("dispatch", function ($event, $dispatcher, $exception) {
                //controller or action doesn't exist
                if ($event->getType() == 'beforeException') {
                    switch ($exception->getCode()) {
                        case \Phalcon\Dispatcher::EXCEPTION_HANDLER_NOT_FOUND:
                        case \Phalcon\Dispatcher::EXCEPTION_ACTION_NOT_FOUND:
                            $dispatcher->forward(array(
                                'controller' => 'index',
                                'action' => 'notFound'
                            ));

                            return false;
                    }
                }
            });

            $dispatcher = new \Phalcon\Mvc\Dispatcher();
            //Set default namespace to backend module
            $dispatcher->setDefaultNamespace("Vokuro\Controllers");
            //Bind the EventsManager to the dispatcher
            $dispatcher->setEventsManager($eventsManager);

            return $dispatcher;
        });