No exceptions are thrown for Non-existing Actions on Phalcon 4

Hi there, On our project, we have recently upgraded phalcon version from 2.0.8 to 4.0.6. Currently, I am not getting any Exceptions for non existing Actions in Controller.. Although I am getting Exceptions for non-existing Controllers as expected. Can anyone assist me on this issue?

Thank you.



909

Hi, don't know if you still have the problem or if it can help.

I use those on my public/index.php

try { 
    // ...
    // doing some stuff.
    // ...

    $application->handle($_SERVER["REQUEST_URI"])->send();
} catch (\Exception $e) {
    echo '<h1>Exception</h1>';
    echo '<pre>' . $e->getMessage() . '</pre>';
    echo '<pre>' . $e->getTraceAsString() . '</pre>';
}

And it throw exception when controller or action are wrong.

Hi Yann, Thanks for your reply. Unfortunately it doesn't work. I get exception when controller is wrong. But the problem is with the actions.

Hi, don't know if you still have the problem or if it can help.

I use those on my public/index.php

try { 
   // ...
  // doing some stuff.
  // ...

   $application->handle($_SERVER["REQUEST_URI"])->send();
} catch (\Exception $e) {
   echo '<h1>Exception</h1>';
   echo '<pre>' . $e->getMessage() . '</pre>';
   echo '<pre>' . $e->getTraceAsString() . '</pre>';
}

And it throw exception when controller or action are wrong.

Use beforeException with the EventManager, it works fine for me
https://docs.phalcon.io/4.0/en/dispatcher

Hi, I am already using beforeException in my code. It perfectly works for both wrong Controllers and actions in 2.0.8 version. But after upgrading to 4.0.6 version, there's no exception for wrong Actions.

$di->set('dispatcher', function () {
    //Create/Get an EventManager
    $eventsManager = new 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::EXCEPTION_HANDLER_NOT_FOUND:
                case \Phalcon\Dispatcher\Exception::EXCEPTION_ACTION_NOT_FOUND:
                    $dispatcher->forward([
                        'controller'    => 'error',
                        'action'        => 'notFound',
                        'params'        => [$exception]
                    ]);
                    return false;
                    break;
                default:
                    $dispatcher->forward([
                        'controller'    => 'error',
                        'action'        => 'index',
                        'params'        => [$exception]
                    ]);
                    return false;
            }
        }
    });

    $dispatcher = new Dispatcher();
    //Bind the EventsManager to the dispatcher
    $dispatcher->setEventsManager($eventsManager);
    return $dispatcher;
});

Use beforeException with the EventManager, it works fine for me
https://docs.phalcon.io/4.0/en/dispatcher



909

Don't know and can't try now if it's change something, but I'm using eventsManager::attach with dispatch:beforeException

$eventsManager->attach(
    'dispatch:beforeException',
    new NotFoundPlugin()
);

And my NotFoundPlugin

public function beforeException(Event $event, MvcDispatcher $dispatcher, \Exception $exception)
    {
        error_log($exception->getMessage() . PHP_EOL . $exception->getTraceAsString());

        if ($exception instanceof MvcDispatcherException) {
            switch ($exception->getCode()) {
                case DispatcherException::EXCEPTION_HANDLER_NOT_FOUND:
                case DispatcherException::EXCEPTION_ACTION_NOT_FOUND:
                    $dispatcher->forward(
                        [
                            'controller' => 'errors',
                            'action'     => 'show404',
                        ]
                    );
                    return false;
            }
        }

        $dispatcher->forward(
            [
                'controller' => 'errors',
                'action'     => 'show500',
            ]
        );

        return false;
    }

I am already using them in my code. My problem is, I am not getting any exception for non exist actions or wrong actions.

Don't know and can't try now if it's change something, but I'm using eventsManager::attach with dispatch:beforeException

$eventsManager->attach(
   'dispatch:beforeException',
   new NotFoundPlugin()
);

And my NotFoundPlugin

public function beforeException(Event $event, MvcDispatcher $dispatcher, \Exception $exception)
   {
       error_log($exception->getMessage() . PHP_EOL . $exception->getTraceAsString());

       if ($exception instanceof MvcDispatcherException) {
           switch ($exception->getCode()) {
               case DispatcherException::EXCEPTION_HANDLER_NOT_FOUND:
               case DispatcherException::EXCEPTION_ACTION_NOT_FOUND:
                   $dispatcher->forward(
                       [
                           'controller' => 'errors',
                           'action'     => 'show404',
                       ]
                   );
                   return false;
           }
       }

       $dispatcher->forward(
           [
               'controller' => 'errors',
               'action'     => 'show500',
           ]
       );

       return false;
   }


909

You're using : $eventsManager->attach("dispatch"

But I'm using :$eventsManager->attach('dispatch:beforeException',

But I don't know if it's really different :/

It's working and shows a 404 page for non existing controller, means I am getting DispatcherException::EXCEPTION_HANDLER_NOT_FOUND. But for wrong actions exception doesn't raise. So, DispatcherException::EXCEPTION_ACTION_NOT_FOUND case is not working.

You're using : $eventsManager->attach("dispatch"

But I'm using :$eventsManager->attach('dispatch:beforeException',

But I don't know if it's really different :/