Solved thread

This post is marked as solved. If you think the information contained on this thread must be part of the official documentation, please contribute submitting a pull request to its repository.

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

http://localhost/phalcon/index

instead

http://localhost/phalcon/

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

Jagg



1.4k
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;
        });


23.4k
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 ;)



1.4k
edited Jul '14

@Flanex:

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



23.4k

@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;
        });