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.

$router->notFound doesn't work

Why it doesn't work?

$router = new Phalcon\Mvc\Router(false);

$router->notFound(array(
      'namespace'=>'Weather/Controllers/Index',
      'controller'=>'index',
      'action'=>'index'
));

Application send exception: PublicfdgdfggdfController handler class cannot be loaded

Thanks



1.4k
Accepted
answer

This is only work if the action is not found, try this:

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


5.5k
edited Jul '14

So sad, but it's working. Thank you.